Adding Arrays From One script to another script's Array

Hi i have a project that has a Two script, and all it does is when the button pressed it tries to find any gameobject with specific tag and put all of the gameobjects under another’s script array!
but i have two problem:

  1. so i created a script like this:
    public GameObject[] targetImageObjectsHere;

    public GameObject teleporter;

    void OnClick () {

        targetImageObjectsHere = GameObject.FindGameObjectsWithTag ("teleportTracker");

        for (int b = 0; b < targetImageObjectsHere.Length; b++) {

            teleporter.GetComponent<Teleporter>().targetImageObjects[b] = targetImageObjectsHere[b];


        }
 
    }

But when i run click on button i get this error but everythings works fine:

UnassignedReferenceException: The variable teleporter of Adder has not been assigned.
You probably need to assign the teleporter variable of the Adder script in the inspector.
UnityEngine.GameObject.GetComponent[Teleporter] ()
Adder.OnClick () (at Assets/Adder.cs:25)
UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
UICamera:Notify(GameObject, String, Object) (at Assets/NGUI/Scripts/UI/UICamera.cs:1469)
UICamera:ProcessRelease(Boolean, Single) (at Assets/NGUI/Scripts/UI/UICamera.cs:2325)
UICamera:ProcessTouch(Boolean, Boolean) (at Assets/NGUI/Scripts/UI/UICamera.cs:2392)
UICamera:ProcessMouse() (at Assets/NGUI/Scripts/UI/UICamera.cs:1830)
UICamera:ProcessTouches() (at Assets/NGUI/Scripts/UI/UICamera.cs:1943)
UICamera:Update() (at Assets/NGUI/Scripts/UI/UICamera.cs:1631)
  1. and my another problem is how can i dynamically change the array size of one script from another scrpt??
    because some times i add more gameobject during the run time!

Thanks!
Please help me

If your collection needs to change size, it should be a List instead of an array.

1 Like

So can you describe more how can i create a list? And how does it works?

Why yes. Here’s a couple of descriptions someone else prepared earlier.

1 Like

Have you thought about what will happen when you add your b elements, and next time the code runs, the b is smaller? There will be a bunch of objects left over from the previous run (b_1 - b_2). If you don’t want this, you can simplify your code a lot:

void OnClick () {
    teleporter.GetComponent<Teleporter>().targetImageObjects = GameObject.FindGameObjectsWithTag ("teleportTracker"); 
}
1 Like

So i made it through with the List but one last problem is i get this error during runtime:

UnassignedReferenceException: The variable teleporter of Adder has not been assigned.
You probably need to assign the teleporter variable of the Adder script in the inspector.
UnityEngine.GameObject.GetComponent[Teleporter] ()
Adder+c__Iterator0.MoveNext () (at Assets/Adder.cs:26)

and my code is like this:

    IEnumerator OnClick ()
    {
        teleporter.GetComponent<Teleporter> ().Tlist.Clear();

        targetImageObjectsHere = GameObject.FindGameObjectsWithTag ("teleportTracker");

        yield return new WaitForSeconds (0.1f);

        for (int b = 0; b < targetImageObjectsHere.Length; b++) {

            teleporter.GetComponent<Teleporter>().Tlist.Add(new Lister(targetImageObjectsHere[b]));

        }
   
    }

and i use delay to clear the list first to make sure dont get same game object duplicated in the list! is there any other way to make sure dont add exicting item again to the list!

Which line is 26?

You should make a variable of teleporter.GetComponent().Tlist to make your code cleaner and minimize GetComponent calls.

Line 26 is:
targetImageObjectsHere = GameObject.FindGameObjectsWithTag (“teleportTracker”);