Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Setting variables on objects instantiated with array

Discussion in 'Scripting' started by Scobbo, Nov 28, 2012.

  1. Scobbo

    Scobbo

    Joined:
    Aug 18, 2012
    Posts:
    43
    Hello,
    I'm trying to instantiate the entire deck of cards all at once here, then assign values to two (public) variables on the card as it is instantiated. I'm probably doing this a very computationally expensive way but that is ok, it's a card game and this is only run once. Esentially unity is saying that it can't find any definition of the variables on the gameobject. Now one of two things is happening here, I can't assign values to variables on gameobjects instantiated with an array, or I have done something wrong. So do I need to have the object reference as an array, or have I missed a step, or something else?

    Here is my code:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Controller : MonoBehaviour {
    5.  
    6. public GameObject cardPrefab;
    7. public GameObject[] cards = new GameObject[10];
    8. public int[,] cardrank = new int[4,18];
    9.  
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         DefineDeck();
    14.         CreateDeck();
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.        
    20.     }
    21.    
    22.     void CreateDeck () {
    23.         //Do this for every card
    24.         for (int i = 0; i < 10; i++)
    25.         {
    26.             //create float versions of ints for the vector3
    27.             float zero = 0f;
    28.             float elevation = (float)i * 0.01f;
    29.             //Instantiate the card in the deck
    30.             cards[i] = Instantiate(cardPrefab, new Vector3(zero, elevation, zero), transform.rotation) as GameObject;
    31.             for (int o = 0; o < 4; o++)
    32.             {
    33.                 for (int p = 0; p < 18; p++)
    34.                 {
    35.                     if (cardrank[o,p] > 0)
    36.                     {
    37.                         cards[i].colour = o;
    38.                         cards[i].rank = p;
    39.                     }
    40.                     else
    41.                     {
    42.                         cardrank[o,p]--;
    43.                     }
    44.                 }
    45.             }
    46.         }
    47.     }
    48.    
    49.     void DefineDeck () {
    50.         //Define every card in the deck
    51.         for (int o = 0; o < 4; o++) //Suit
    52.         {
    53.             for (int p = 0; p < 18; p++) //Rank
    54.             {
    55.                 cardrank[o,p] = 2;
    56.             }
    57.         }
    58.     }
    59.    
    60.     void Shuffle () {
    61.     }
    62. }
    Thanks! :)
     
  2. BPPHarv

    BPPHarv

    Joined:
    Jun 9, 2012
    Posts:
    318
    You're storing a reference to the new cards GameObject when you instantiate but GameObjects don't have a colour or rank property. It's likely one of the scripts attached to the object has these variables. To set those I think you need to do a GetComponent after you instantiate to get a reference to the newly created object's script.

    Code (csharp):
    1.  
    2. //Obtain reference to a script called "cardScript" which is a script on the instantiated object.
    3. cards[i] = Instantiate(cardPrefab, new Vector3(zero, elevation, zero), transform.rotation) as GameObject;
    4. cardScript thiscard = cards[i].GetComponent(cardScript);
    5.  
    then later on in your loop you set rank and colour like this
    Code (csharp):
    1.  
    2. thiscard.colour=o;
    3. thiscard.rank=p;
    4.  
    Edit: The above code is untested as I'm not near a Unity install to pre-test.
     
  3. Scobbo

    Scobbo

    Joined:
    Aug 18, 2012
    Posts:
    43
    Of course... Thanks for the help!