Skip to main content
commented code
Source Link
dval
  • 426
  • 1
  • 3
  • 11

The old Unity docs have some examples. And the Unity Answers site is generally a better place to look for such things. But...

Basically, you need to reference the other gameobject and use GetComponent<YourScript>()

So, in the script your working on, you need to declare a public variable that you can assign in the inspector, or you need to use GameObject.Find("OtherGameObjectByName");

If you look at this setup, you can see two gameobjects with each with there own component/script attached. I am using this to illustrate the following code.

unity example

The 'ControlObject.cs' attached to 'GameObjectController'.

public class ControlObject : MonoBehaviour {

    // There is a better place for this, other than the Start method.
    // but I'm lazy.
    void Start () { 

        // This will look for a specific game object.
        GameObject mygo = GameObject.Find("GameObjectOther");
        // if that game object is found...
        if(mygo){ 

             // you can access any of its components.
             DynamicObject otherguy = mygo.GetComponent<DynamicObject>(); 

             //do stuffNow you can modify public properties or call methods
             otherguy.scoreCount++;
        }
    }
}

The 'DynamicObject.cs' attached to 'GameObjectOther'.

public class DynamicObject : MonoBehaviour { 

    //make sure you have a public propertey
    public int scoreCount = 0;

    // 
    void Update () {
        //Do something with scoreCount.

    }
}

The old Unity docs have some examples. And the Unity Answers site is generally a better place to look for such things. But...

Basically, you need to reference the other gameobject and use GetComponent<YourScript>()

So, in the script your working on, you need to declare a public variable that you can assign in the inspector, or you need to use GameObject.Find("OtherGameObjectByName");

If you look at this setup, you can see two gameobjects with each with there own component/script attached. I am using this to illustrate the following code.

unity example

The 'ControlObject.cs' attached to 'GameObjectController'.

public class ControlObject : MonoBehaviour {

    // There is a better place for this, other than the Start method.
    // but I'm lazy.
    void Start () {
        GameObject mygo = GameObject.Find("GameObjectOther");
        if(mygo){
           DynamicObject otherguy = mygo.GetComponent<DynamicObject>();
            //do stuff
            otherguy.scoreCount++;
        }
    }
}

The 'DynamicObject.cs' attached to 'GameObjectOther'.

public class DynamicObject : MonoBehaviour {

    public int scoreCount = 0;

    // 
    void Update () {
        //Do something with scoreCount.

    }
}

The old Unity docs have some examples. And the Unity Answers site is generally a better place to look for such things. But...

Basically, you need to reference the other gameobject and use GetComponent<YourScript>()

So, in the script your working on, you need to declare a public variable that you can assign in the inspector, or you need to use GameObject.Find("OtherGameObjectByName");

If you look at this setup, you can see two gameobjects with each with there own component/script attached. I am using this to illustrate the following code.

unity example

The 'ControlObject.cs' attached to 'GameObjectController'.

public class ControlObject : MonoBehaviour {

    // There is a better place for this, other than the Start method.
    // but I'm lazy.
    void Start () { 

        // This will look for a specific game object.
        GameObject mygo = GameObject.Find("GameObjectOther");
        // if that game object is found...
        if(mygo){ 

             // you can access any of its components.
             DynamicObject otherguy = mygo.GetComponent<DynamicObject>(); 

             // Now you can modify public properties or call methods
             otherguy.scoreCount++;
        }
    }
}

The 'DynamicObject.cs' attached to 'GameObjectOther'.

public class DynamicObject : MonoBehaviour { 

    //make sure you have a public propertey
    public int scoreCount = 0;

    // 
    void Update () {
        //Do something with scoreCount.

    }
}
Source Link
dval
  • 426
  • 1
  • 3
  • 11

The old Unity docs have some examples. And the Unity Answers site is generally a better place to look for such things. But...

Basically, you need to reference the other gameobject and use GetComponent<YourScript>()

So, in the script your working on, you need to declare a public variable that you can assign in the inspector, or you need to use GameObject.Find("OtherGameObjectByName");

If you look at this setup, you can see two gameobjects with each with there own component/script attached. I am using this to illustrate the following code.

unity example

The 'ControlObject.cs' attached to 'GameObjectController'.

public class ControlObject : MonoBehaviour {

    // There is a better place for this, other than the Start method.
    // but I'm lazy.
    void Start () {
        GameObject mygo = GameObject.Find("GameObjectOther");
        if(mygo){
           DynamicObject otherguy = mygo.GetComponent<DynamicObject>();
            //do stuff
            otherguy.scoreCount++;
        }
    }
}

The 'DynamicObject.cs' attached to 'GameObjectOther'.

public class DynamicObject : MonoBehaviour {

    public int scoreCount = 0;

    // 
    void Update () {
        //Do something with scoreCount.

    }
}