0
\$\begingroup\$

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);



        }

enter image description here

\$\endgroup\$
5
  • \$\begingroup\$ Looks good to me. Are both scripts on the same gameObject?. What's the exact error? \$\endgroup\$ Jun 16 '17 at 11:54
  • \$\begingroup\$ No they are attached to different gameobject \$\endgroup\$ Jun 16 '17 at 11:57
  • \$\begingroup\$ There isn't any error, its just that the object carries null value. \$\endgroup\$ Jun 16 '17 at 11:59
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ Jun 16 '17 at 12:04
  • \$\begingroup\$ 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? \$\endgroup\$ Jun 16 '17 at 12:07
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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>();
\$\endgroup\$
5
  • \$\begingroup\$ I think this pretty much answers the question. Why's the downvote? \$\endgroup\$ Jun 16 '17 at 13:11
  • \$\begingroup\$ I didn't downvote, but there is a slight change that worked for me. GameObject.FindWithTag instead of GameObject.Find. Thanks anyway :) \$\endgroup\$ Jun 16 '17 at 15:09
  • \$\begingroup\$ 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. \$\endgroup\$ Jun 19 '17 at 6:53
  • \$\begingroup\$ 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. \$\endgroup\$
    – Gnemlock
    Jul 17 '17 at 9:18
  • \$\begingroup\$ Giving this a +1 because GameObject.Find will work. The caveat is that it shoudn't be used in Update() \$\endgroup\$ Dec 28 '17 at 19:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .