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
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
winText.text = "";
SetCountText ();
GameObject[] pickUpArray = GameObject.FindGameObjectsWithTag ("Pick Up");
}
void Update () //Change color of the cubes when 6 cubes are left
{
if (count >= 6 && count < 13)
{
foreach (GameObject go in pickUpArray)
{
go.GetComponent<Renderer> ().material.color = Color.red;
}
}
}
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.
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?
public class TurnWheels : MonoBehaviour
{
public Transform[] front;
public Transform[] rear;
public static float angle = 30f;
private int ori; //orientation
private int currOri; //current orientation
private float[] rotBy = { angle, 0, -angle }; //defining angle array
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()