Search Unity

Help with accessing variable from Start() and using that in Update()

Discussion in 'Getting Started' started by buzzo, Feb 22, 2017.

  1. buzzo

    buzzo

    Joined:
    Feb 21, 2017
    Posts:
    7
    Hi I am trying to create GameObject type array in Start function and trying to access that variable array in Update function but Unity complier says " The variable `pickUpArray' is assigned but its value is never used ".
    But when I put this code :
    GameObject[] pickUpArray = GameObject.FindGameObjectsWithTag ("Pick Up");

    inside the Update function it simply runs
    Code (CSharp):
    1. void Start()
    2.     {
    3.         rb = GetComponent<Rigidbody>();
    4.         count = 0;
    5.         winText.text = "";
    6.         SetCountText ();
    7.         GameObject[] pickUpArray = GameObject.FindGameObjectsWithTag ("Pick Up");
    8.  
    9.     }
    10.  
    11.     void Update ()  //Change color of the cubes when 6 cubes are left
    12.     {
    13.         if (count >= 6 && count < 13)
    14.         {
    15.             foreach (GameObject go in pickUpArray)
    16.             {
    17.              
    18.                 go.GetComponent<Renderer> ().material.color = Color.red;
    19.             }
    20.         }
    21.     }
     
  2. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    That's because pickUpArray as used are local variables. Try defining pickUpArray as private above Start. Also it's a good idea to verify find actually found something before using.
     
    buzzo and JoeStrout like this.
  3. buzzo

    buzzo

    Joined:
    Feb 21, 2017
    Posts:
    7
    Okay thanks mate got it working fine
     
  4. Ferrick

    Ferrick

    Joined:
    Jul 8, 2022
    Posts:
    2
    Hi, I had a similar issue and moved defining the array to above the start as you said, and it works but only if I set "angle" to be a static variable as I've done here.

    But I want to be able to adjust the angle variable in the inspector. Setting it to static prevents this, but the compiler throws an error about the fact that i'm trying to define an array with non-static variables if i don't:
    " error CS0236: A field initializer cannot reference the non-static field, method, or property 'TurnWheels.angle' "

    Any ideas on a solution?

    Code (CSharp):
    1. public class TurnWheels : MonoBehaviour
    2. {
    3.     public Transform[] front;
    4.     public Transform[] rear;
    5.     public static float angle = 30f;
    6.     private int ori;                                //orientation
    7.     private int currOri;                            //current orientation
    8.     private float[] rotBy = { angle, 0, -angle };   //defining angle array
    9.  
     
  5. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Initialize your
    rotBy
    array in
    Awake
    or
    Start
    instead. You can then remove the
    static
    modifier from
    angle
    .
    Code (CSharp):
    1. public class TurnWheels : MonoBehaviour
    2. {
    3.     public Transform[] front;
    4.     public Transform[] rear;
    5.     public static float angle = 30f;
    6.     private int ori; //orientation
    7.     private int currOri; //current orientation
    8.     private float[] rotBy;
    9.  
    10.     void Awake()
    11.     {
    12.         rotBy = new float[] { angle, 0, -angle };
    13.     }
    14. }
     
    Ferrick likes this.
  6. Ferrick

    Ferrick

    Joined:
    Jul 8, 2022
    Posts:
    2
    Ah okay, thank you. I've set it up as shown and it's working now.

    I think when I'd attempted previously to initialise the array within Start(), I had constructed it incorrectly so that it only appeared locally within Start()

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         float [] rotBy = new float[] { angle, 0, -angle };
    Thanks for your help!