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.