Referencing Arrays

Sorry if this is a stupid question. I am sure the answer quite evident in documentation but I don’t even know how to put what I am looking for into words. I would like to have a number of arrays and them reference them by a variable. The code below illustrates the idea of what I am trying to achieve, but I am aware that this is not the correct way to do it. Can anyone point me in the right direction?

public class PretendScript : MonoBehaviour
{
     void Awake()
     {
          String[] fruits = {"apples", "bananas", "oranges"};
          String[] veggies = {"carrots", "peas", "broccoli"};
          Array activeArray = fruits;
          foreach(string item in activeArray)
          {
               Debug.Log(item);
               //I would like it to say: apples, bananas, oranges
          }
     }
}

Arrays are a reference type, so you can say
string[ ] activeArray = fruits;
and you’ll be accessing the same array, including any changes made to array after you set activeArray = fruits.

However, if you later set fruits to an entirely new array, that won’t automatically affect activeArray. If you need a symbolic link to the variable, rather than to the array, then you’d need to add another layer of indirection (e.g. by creating a class that contains an array, instead of using an array directly).

1 Like

Awesome, thank you very much! I was struggling with this one. So for anyone who is like me and couldn’t figure this out, here is how it should look:

public class PretendScript : MonoBehaviour
{
     void Awake()
     {
          string[] fruits = {"apples", "bananas", "oranges"};
          string[] veggies = {"carrots", "peas", "broccoli"};
          string[] activeArray = fruits;
          foreach(string item in activeArray)
          {
               Debug.Log(item);
          }
     }
}

One of the most confusing things that will get you in C# at some point when you’re a beginner is the idea that you need to write Clone() methods for your classes, or else you’ll be referencing the values instead of creating new instances of them. This will have you pulling your hair out on long nights with bugs:

[System.Serializable] // < - so you can see variables in inspector
public class Character {

     public string Name;

    [System.Serializable]
    public class Stats {
       public int HP;
       public int Mana;

       public void Clone() {
            HP = this.HP;
            Mana = this.Mana;
        }
   }
   public Stats stats = new Stats();

   public Character GetClone() {
         character.Name = this.Name;
         character.stats = this.stats.Clone();
   }
}

Now you can do:

Character RobotGuy = new Character();
Character Fred = new Character();

Fred = RobotGuy.GetClone();

And you clone the guys stats.

If you just do:

Fred = RobotGuy;

Now when you change Fred’s HP:

Fred.stats.HP = 5;

RobotGuy’s HP will also be 5 now. This’ll have you going through old code for hours on end. When I first made a turn based battle system for my RPG, all of the characters had references graphics, so it came out as 4 of the same guy, and it took me an entire day to figure it out.

And to make things more complex, a ‘reference’ in C# also refers to when you pass the ref keyword to method so you can change values on something:

public void ChangeHPToFive(ref Character RobotGuy) {
     RobotGuy.stats.HP 5;
}

//vs

public Character ChangeHPToFive(Character RobotGuy) {
     RobotGuy.stats.HP 5;
     return RobotGuy;
}

Which both do the same thing, but you don’t have to return a value with the first one because of the ref keyword.

This took me a while to grasp but it’s necessary so you’re not just referencing values. I’m also still a beginner to intermediate programmer myself so there may be a better way.

1 Like