# Access gameObject array value from other script

I'm trying to access gameobject array values from one script to another. I have attached certain gameobject capsules with the array in the editor as shown in the figure below and I'm trying to access the values of array to another script. But I get null values when i try to pass the values. I don't want to use gameobject tag since i have to set gameobject active and deactive during runtime.

PinsScript.cs

public class PinsScript : MonoBehaviour {

// Use this for initialization
public GameObject[] pins;
void Start () {

}

// Update is called once per frame
void Update () {

}
}


MovingBall.cs

PinsScript p = GetComponent<PinsScript>();

foreach (GameObject pin in p.pins)
{

pin.SetActive(false);

}


• Looks good to me. Are both scripts on the same gameObject?. What's the exact error? – Aditya Yadav Jun 16 '17 at 11:54
• No they are attached to different gameobject – Salar Muhammad Jun 16 '17 at 11:57
• There isn't any error, its just that the object carries null value. – Salar Muhammad Jun 16 '17 at 11:59
• Obviously. If they're not attached to the same gameObject, then the statement PinsScript p = GetComponent<PinsScript>(); is wrong. Because, it's trying to find the PinsScript component in the same gameObject, which is not there, as you said. – Aditya Yadav Jun 16 '17 at 12:04
• Well the condition is different here, I have to run the Movingball script at runtime and not at the start of the game. Is there other way that i can proceed with? – Salar Muhammad Jun 16 '17 at 12:07

Attach a PinScript to each pin. Keep a reference to each of your pins in an array when you Awake. Your PinScript can be empty. Then you have a reference to turn them on or off as you please. Your scripts would look like this:

PinScript.cs

public class PinsScript : MonoBehaviour { }


MovingBall.cs

public class MovingBall : MonoBehaviour
{
private PinsScript[] pinScripts;

private void Awake()
{
pinScripts = FindObjectsOfType<PinsScript>();
}

public void SetPinActivity(bool active)
{
foreach (var pin in pinScripts)
{
pin.gameObject.SetActive(active);
}
}
}


Then you can deactivate them by calling:

myMovingBall.SetPinActivity(false);


This, however, is not a great architecture. But it is a simple solution.

I assume you do not know how many pins you have. If you know your pins ahead of time, let me know, and I will suggest a more optimized solution. But this will work either way.

Your problem here is with referencing which PinsScript are you trying to access.

Whenever you are instanciating a new MovingBall, you must reference the PinsScript somewhere. I usually do this by adding a small portion of code on the MonoBehavior which create the MovingBall :

public PinsScript pinsScript;


Then, you just have to drag the GameObject holding the PinsScript into that variable. Another way to do it is by accessing the GameObject by name :

PinsScript p = GameObject.Find("TheNameOfTheGameObjectHoldingThePinsScript").GetComponent<PinsScript>();

• I think this pretty much answers the question. Why's the downvote? – Aditya Yadav Jun 16 '17 at 13:11
• I didn't downvote, but there is a slight change that worked for me. GameObject.FindWithTag instead of GameObject.Find. Thanks anyway :) – Salar Muhammad Jun 16 '17 at 15:09
• Both GameObject.FindWithTag and GameObject.Find will work. The difference is that the former takes the Name of the Object as argument while the latter takes a tag. – Aditya Yadav Jun 19 '17 at 6:53
• Again, I didnt downvote; but GameObject.Find is often persuaded against, as it is inefficient and laggy. Users may find that this is not an appropriate solution, at a certain number of gameobjects. – Gnemlock Jul 17 '17 at 9:18
• Giving this a +1 because GameObject.Find will work. The caveat is that it shoudn't be used in Update() – Draco18s no longer trusts SE Dec 28 '17 at 19:47