Changing variable of Instantiated GameObject is changing variable within script AND other GameObjects

So basically what I am trying to do is create a GameObject (lets call it GO1) from a prefab. This gameobject has a script attached. I am setting a variable within that script to a certain value.

That all works fine, the problem is when I create a new gameobject in the same manner as above and change the variable of this gameobjects (GO2) attached script, GO1’s variable changes also. It isn’t a static variable. I’ve been trying to find the problem for a day and I cant find it.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PopulationScoreManager : MonoBehaviour
{
BugBehaviour.Neuron neuralNetworkss = new BugBehaviour.Neuron[2];
int pop;
int generation = 1;
public GameObject bug;
float xLocation;
float yLocation;
public static int score;
Text text;

void Start()
{
    text = GetComponent<Text>();
    score = 100;
}

void Update()
{
    if (score < 0)
    { score = 0; }
    
    if (score == 0)
    {
        spawnGeneration();

    }
    text.text = "" + score;
}

public static void AddPoints(int pointsToAdd)
{
    score += pointsToAdd;
}

public static void Reset()
{
    score = 0;
}

public void sendNeuralNetwork(BugBehaviour.Neuron[][] neuralNetworkp)
{
    neuralNetworkss = neuralNetworkp;
}

public void spawnGeneration()
{
    for (int i = 0; i < 2; i++)
    {
    xLocation = Random.Range(0f, 137f);
    yLocation = Random.Range(-130f, 3f);
    GameObject g = Instantiate(bug, new Vector3(xLocation,yLocation,0), Quaternion.identity) as GameObject;
    BugBehaviour behave = g.GetComponent<BugBehaviour>();
    behave.firstGeneration = false;
    behave.neuralNetwork = neuralNetworkss;
    behave.outputNeuralNetwork();
    behave.neuralNetwork = behave.mutateNeuralNetwork();
    behave.outputNeuralNetwork();
    AddPoints(1);
    }
    GenerationScoreManager.SendGen(GenerationScoreManager.score + 1);
}

}

The points of interest are within the spawnGeneration() method.
The outputNeuralNetwork() method outputs variable “neuralNetwork” to the console so that I can see, I’ve attached an example of the output

.

It appears that neuralNetworkss is getting re declared in between objects. But it is not. What is going on? ! :S

Thanks, Dave.

It looks like you are declaring methods and variables using the “static” key word. This means that every instance of this script would share these variables and methods. See static modifier - C# Reference | Microsoft Learn

Try removing the key word static from “score” and “AddPoints”. You may need to change whatever is calling “AddPoints”.

If you need to access these members from another script, either do a SendMessage to the gameobject or use gameObject.GetComponent();

Well, if you don’t create a new instance of your neuralnetwork all will use the same. What exactly does “mutateNeuralNetwork” do? Does it create a new instance by copying the old values and mutate the new instance, or does it simply mutate the current instance?

You asked in the comment:

Are classes passed by reference in C#?

Of course. Classes are reference types. Everything that lives on the heap is a reference type. Therefore when you assign or pass a reference around, you will always reference the same object instance.

Btw: the concept of references and “passing by reference” are two complete seperate things. By default all parameters are passed by value. However the value of “reference types” is the reference. So even you pass the reference by value you still reference the same object. In C# classes are always reference types while structs a value types.

There was another question some time ago about neural networks where i implemented a general purpose ANN. It’s just a quick and dirty implementation but if you’re stuck somewhere you might want to have a look.