How do I modify values in an array from a different C# script ?
Hi . so , i'm trying to generate an array of int's in Managerscript.cs and then pass it's values to another gameobject with another script called AIgen2.cs and no method seems to work Here's some code :
public class Managerv2 : MonoBehaviour
{
public GameObject Agent;
public int numberofagents;
public bool startbool;
public GameObject[] agentlist;
public int[] movementvector;
public int[] rotationvector;
public Rigidbody[] rb;
public int numberofiterations;
// Start is called before the first frame update
void Start()
{
numberofagents = 5;
numberofiterations = 300;
startbool = false;
agentlist = new GameObject[numberofagents];
movementvector = new int[numberofiterations];
rotationvector = new int[numberofiterations];
rb = new Rigidbody[numberofagents];
}
void GenerateMovementandRotation()
{
for (int i = 0; i <numberofiterations; i++)
{
int speedfactor = Random.Range(0, 16);
movementvector[i] = speedfactor;
int rotfactor = Random.Range(-360, 360);
rotationvector[i] = rotfactor;
}
}
void StartGeneration()
{
for (int i = 0; i < numberofagents; i++)
{
GenerateMovementandRotation();
for (int j = 0; j < numberofiterations; j++)
{
AIgen2 agentz = agentlist[i].GetComponent<AIgen2>();
agentz.movementvector2[j] = movementvector[j];
//agentlist[i].GetComponent<AIgen2>().rotationvector2[j] = rotationvector[j];
}
}
}
The agentlist[i] it's already build and has it's values . Everything works ok , no errors except the fact that none of the values are transferred from rotationvector to rotationvector2
[[[ agentlist[i].GetComponent().rotationvector2[j] = rotationvector[j]; ]]]
I did check that the array movementvector and rotationvector have values in them before trying to pass them to movementvector2 and rotationvector2 .
Second script
public class AIgen2 : MonoBehaviour
{
public int k = 0;
public bool startgeneration = false;
public GameObject goal;
public GameObject manager;
public Managerv2 managerscript;
public int numberofiterations;
public int numberofagents;
public int[] movementvector2;
public int[] rotationvector2;
public Rigidbody rb;
}
private void Awake()
{
manager = GameObject.FindGameObjectWithTag("manager");
managerscript = manager.GetComponent<Managerv2>();
numberofagents = managerscript.numberofagents;
numberofiterations = managerscript.numberofiterations;
movementvector2 = new int[numberofiterations];
rotationvector2 = new int[numberofiterations];
rb = GetComponent<Rigidbody>();
goal = GameObject.FindGameObjectWithTag("goal");
}
}
All the arrays have the same length . I just can't . help me out please
Answer by tormentoarmagedoom · Oct 02, 2019 at 08:04 AM
Hello there. I think you have some confusion about scriping. Lets say you have this array in script A, that is in ObjectA:
float[] Array1;
If you want to refear to that array, all the array, (not one value of it), dont have to declare the " [ ] " symbols, because that is only to specify one element inside the array.
Lets make a 2nd array in the same Script A that will be exactly the same as Array1
float [] Array2 = Array1;
Now, array 2 its exactly the same as Array1, same lenght, same values, they all in ObjectA.
So now, lets imagine we have a ObjectB with script B and want to make a Array3 and Array4 copies of the arrays of scriptA:
float[] Array3 = ObjectA.GetComponent<ScriptA>().Array1;
float[] Array4 = ObjectA.GetComponent<ScriptA>().Array2;
As you see, i used the "[ ]" symbols only for declare the array. but not for define it, (because the variable called Array3 is an array, is not just a value) We could make it also like this to be more "realistic":
float[] Array3;
float[] Array4
GameObject ObjectA;
void Start()
{
ObjectA = GameObject.Find("ObjectA");
Array3 = ObjectA.GetComponent<ScriptA>().Array1;
Array4 = ObjectA.GetComponent<ScriptA>().Array2;
}
When you use the "[ ]" symbols, is to refear a value inside an array. If you dont use the symbols you are refearing to whole array. I expect you to understand all, if not, please ask again, specify what you dont understand
Bye!
Your stuff worked but let me explain why it wasn't working for me . what i did wrong is :
managescript2.cs:
float [] array1=new float[n]; // THIS HAS VALUES I NEED
GetComponent<aigen2>().array2 = array1;
aigen2.cs
float []array2=new float[n]; /THIS IS WHERE THE VALUES need TO BE TRANSFERRED
Ins$$anonymous$$d i should have done
managescript2.cs:
float [] array1=new float[n]; // THIS HAS VALUES I NEED
aigen2.cs
float []array2=new float[n]; /THIS IS WHERE THE VALUES need TO BE TRANSFERRED
array2 =GetComponent<managerscript2>().array1 ;
You are supposed to move the values to the script that has the array you need filled ins$$anonymous$$d of trying to access and modify it(the array) from another script .
so what? You get what you want? or not?
The thing is to wknow what is beeing executed first.
when you do
new float[n]
its changing all values of the array to 0. so you must be sure this is done before you set all the values, or do not define the array, just declare it.
declare:
float [] array1;
define:
array1 = new float[n];
You can just declare it and dont define, so when the other script put inside all the values, you are sure they will be not replaced by zeros.
(PS: upvota and accept answer then, so i can close the post)
Bye!