Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Accessing Instantiated Object's Variables...

Discussion in 'Scripting' started by LegendXV, Jun 2, 2015.

  1. LegendXV

    LegendXV

    Joined:
    May 31, 2015
    Posts:
    30
    I was able to access the component's of the instantiated variables script, but when I made a multitude of them they all returned the same value ...

    Code (csharp):
    1.  
    2.     private void createClickables() {
    3.        
    4.         // Set playmode
    5.         currentMode = playModes[Random.Range (0, playModes.Length)];
    6.  
    7.         // Create Clickables
    8.         int numVals = Random.Range (6, 10);
    9.         for (int x = 0; x < numVals; x++) {
    10.             GameObject go = Instantiate(prefab, new Vector3(Random.Range (-4,4), 1, Random.Range (-4,4)), Quaternion.identity) as GameObject;
    11.             Clickable clicky = go.GetComponent<Clickable>();
    12.             print (clicky.number);
    13.             list.Add (clicky.GetNumber ());
    14.             clicky.initiate(this, Random.Range (0,10));
    15.         }
    16.         print ("Current List.");
    17.         /*
    18.         for (int x = 0; x < list.Count; x++) {
    19.             print (list[x]);
    20.         }*/
    21.     }
    22.  
    So the key part is here ...

    Code (csharp):
    1.  
    2. GameObject go = Instantiate(prefab, new Vector3(Random.Range (-4,4), 1, Random.Range (-4,4)), Quaternion.identity) as GameObject;
    3.             Clickable clicky = go.GetComponent<Clickable>();
    4.             print (clicky.number);
    5.             list.Add (clicky.GetNumber ());
    6.             clicky.initiate(this, Random.Range (0,10));
    7.  
    When I print the number it prints 0, but when I click on the object during playmode it returns the number it was bound with. This holds true for all the objects I instantiated.
     
  2. LegendXV

    LegendXV

    Joined:
    May 31, 2015
    Posts:
    30
    Nvm, found the bug.