Quantcast
Viewing latest article 11
Browse Latest Browse All 35

Answer by hangemhigh

You are trying to access another function from another class. You can't do MoveCubes.Movecubes(); unless the function you want to call from another class is static. Example of a static function is as below. public static void Movecubes(){ Vector3 posCopy = tranform.position; // make a copy with all the previous values for x/y/z. posCopy.y = 1; // make modification transform.position = posCopy; // change position to now copy. } Now you can do MoveCubes.Movecubes(); This will work for c#. In unity, it is different. To access another function without any errors using Unity, the script must be attached to a game object. To get this to work. 1) Create an empty gameobject named "Whatever" 2) Attach the MoveCubes script to the "Whatever" game object.(by drag and drop the script to the game object) 3) In your WightedCube class, change public class WightedCube : MonoBehaviour { void OnTriggerEnter(Collider other) { if (other.transform.tag == "Pad") { MoveCubes.Movecubes(); } } } to public class WightedCube : MonoBehaviour { private MoveCubes myMovesCubes; void Awake(){ /*Find the "Whatever" game object we created then get the "MoveCubes" script attached to it*/ myMovesCubes = GameObject.Find("Whatever").GetComponent(); } void OnTriggerEnter(Collider other) { if (other.transform.tag == "Pad") { myMovesCubes.Movecubes();//call the function using the reference } } }

Viewing latest article 11
Browse Latest Browse All 35

Trending Articles