- Home /
How can I Instantiate a GameObject and then alter an array that is attached to that GameObject
I can currently Instantiate the GameObject just fine, but I have a script attached to it that has an array for waypoints that I would like the object to move back and forth to. In the editor I was able to manually set waypoints by dragging empty gameobjects into the array, but I would prefer to instantiate these empty objects and set their transform.position via this BuildLevel script instead. This is the part I am stuck on. The goal is to be able to have multiple enemies each with their own waypoints to patrol.
Here is the code I am using to instantiate my GameObject(some code is snipped):
public GameObject Enemy2Way;
public void BuildLevel(int whatLevel)
{
if (whatLevel == 0)
{
Instantiate (Enemy2Way, new Vector3 (0, 1, 5), Quaternion.identity);
}
}
And here is the script that is attached to the Enemy2Way gameobject:
public Transform[] patrolPoints;
private int currentPoint;
public float moveSpeed;
void Start () {
transform.position = patrolPoints [0].position;
currentPoint = 0;
}
void Update () {
if (transform.position == patrolPoints [currentPoint].position)
{
currentPoint++;
}
if (currentPoint >= patrolPoints.Length)
{
currentPoint = 0;
}
transform.position = Vector3.MoveTowards (transform.position, patrolPoints [currentPoint].position, moveSpeed * Time.deltaTime);
}
}
Answer by tanoshimi · Sep 02, 2014 at 05:49 PM
Change:
Instantiate (Enemy2Way, new Vector3 (0, 1, 5), Quaternion.identity);
To:
GameObject Temp = Instantiate (Enemy2Way, new Vector3 (0, 1, 5), Quaternion.identity) as GameObject;
Temp.GetComponent<WhateverTheNameOfYourSecondScriptIs>().patrolPoints = new Transform[...];
Replacing ... with your desired array of transforms.
I get this message when trying your approach:
An object reference is required to access non-static member `EnemyControl.patrolPoints'
EnemyControl is the name of my second script in this case. I also tried setting the patrolPoints variable to:
public static Transform[] patrolPoints;
But it still prompted that error. For reference this is my updated code based on your example.
public GameObject patrolPoint;
GameObject enemy1 = Instantiate (Enemy2Way, new Vector3 (0, 1, 5), Quaternion.identity);
enemy1.GetComponent(EnemyControl.patrolPoints[0] = Instantiate (patrolPoint, new Vector3 (0, 1, 0), Quaternion.identity));
I put in the public GameObject as I was unsure as how to use Transform exactly as you stated in your example as it was different to what I was doing before.
I seem to be unsure as to what goes into the Transform[...] What I was doing before doesn't seem to apply in this case and I seem to be stuck figure out what needs to be put in there. To clarify, what I did previously was to create an empty gameobject modify its transform via the GUI and it magically worked. Scripting those steps seem to be more complicated.
Your syntax is wrong - take a look at my answer again. Your code should be:
GameObject enemy1 = Instantiate (Enemy2Way, new Vector3 (0, 1, 5), Quaternion.identity);
enemy1.GetComponent().patrolPoints[0] = Instantiate (patrolPoint, new Vector3 (0, 1, 0), Quaternion.identity)).gameObject.transform;
Ack - stupid UA won't let me post angled brackets in comments :(
I looked up the GetComponent and I think I understand that part now. What I am using now is:
enemy1.GetComponent<EnemyControl>().patrolPoints[0] = Instantiate (patrolPoint, new Vector3 (0, 1, 0), Quaternion.identity).gameObject.transform;
$$anonymous$$onodevelop doesn't seem to like the .gameObject.transform bit at the end for some reason.
Should I ins$$anonymous$$d instantiate a new gameobject first and then do: ~~~.patrolPoints[0] = newgameobject.gameobject.transform;
Answer by Fohristiwhirl · Sep 02, 2014 at 07:00 PM
It might be better to create a method in the EnemyControl script that initialises the array. Using this approach you ensure that the array is handled the way that the EnemyControl script expects. You can do this by making the array private and adding a public initialization method. Then you would just use:
GameObject go = Instantiate(Enemy2Way, new Vector3(0,1,5), Quaternion.identity);
(EnemyControl)go.GetComponent(typeof(EnemyControl)).InitializeWaypoints(4); //4 is the number of waypoints to be initialized
Use of c# generic list might also be easier.
Your answer
