Skip to main content

I just learned that in a C# tutorial, that we can declare var name = new ClassNameOfOtherScript();var name = new ClassNameOfOtherScript(); to create a new class object, then use the object to call the function in that class, but it don'tdoesn't seem to be working in unityUnity. 

Am I understandunderstanding it wronglywrong or in unity itsdoes it work differently in Unity? 

I have a class Cube1,Cube1 this is attached to an object with spriterendererSpriteRenderer enabled. Then I have another script(clock controller) ClockController which is not attach to any object. When I run it, the color of cube does not change to red?

 public class Cube1 : MonoBehaviour
{

public void Rectangle()
 {
    GetComponent<SpriteRenderer>().color = Color.red;
 }

}

public class ClockController : MonoBehaviour

{

 void Start()
{
    var cube1 = new Cube1();
    cube1.Rectangle();
}
public class Cube1 : MonoBehaviour
{
    
    public void Rectangle()
    {
        GetComponent<SpriteRenderer>().color = Color.red;
    }

}

}

public class ClockController : MonoBehaviour
{
  
    void Start()
    {
        var cube1 = new Cube1();
        cube1.Rectangle();
    }
}

I just learned that in C# tutorial, we can declare var name = new ClassNameOfOtherScript(); to create a new class object, then use the object to call the function in that class but it don't seem to be working in unity. Am I understand it wrongly or in unity its work differently? I have a class Cube1, this is attached to an object with spriterenderer enabled. Then I have another script(clock controller) which is not attach to any object. When I run it, the color of cube does not change to red?

 public class Cube1 : MonoBehaviour
{

public void Rectangle()
 {
    GetComponent<SpriteRenderer>().color = Color.red;
 }

}

public class ClockController : MonoBehaviour

{

 void Start()
{
    var cube1 = new Cube1();
    cube1.Rectangle();
}

}

I just learned in a C# tutorial that we can declare var name = new ClassNameOfOtherScript(); to create a new class object, then use the object to call the function in that class, but it doesn't seem to be working in Unity. 

Am I understanding it wrong or does it work differently in Unity? 

I have a class Cube1 this is attached to an object with SpriteRenderer enabled. Then I have another script ClockController which is not attach to any object. When I run it, the color of cube does not change to red?

public class Cube1 : MonoBehaviour
{
    
    public void Rectangle()
    {
        GetComponent<SpriteRenderer>().color = Color.red;
    }

}
public class ClockController : MonoBehaviour
{
  
    void Start()
    {
        var cube1 = new Cube1();
        cube1.Rectangle();
    }
}
Source Link
chuackt
  • 253
  • 2
  • 12

How to Call a function from another script?

I just learned that in C# tutorial, we can declare var name = new ClassNameOfOtherScript(); to create a new class object, then use the object to call the function in that class but it don't seem to be working in unity. Am I understand it wrongly or in unity its work differently? I have a class Cube1, this is attached to an object with spriterenderer enabled. Then I have another script(clock controller) which is not attach to any object. When I run it, the color of cube does not change to red?

 public class Cube1 : MonoBehaviour
{

public void Rectangle()
 {
    GetComponent<SpriteRenderer>().color = Color.red;
 }

}

public class ClockController : MonoBehaviour

{

 void Start()
{
    var cube1 = new Cube1();
    cube1.Rectangle();
}

}