- Home /
Alter a GameObjects(Clone) array from another script C#
I have a prefab Enemy2Way GameObject that I instantiate from my GameManager script. The Enemy2Way object has a script attached to it that controls waypoint movement. I control this via public static Transform[] patrolPoints array. Each point in the array should be a waypoint that the enemy patrols to. I am trying to instantiate empty gameobjects as the patrol points in my GameManager script during a level build function. I am able to instantiate the enemy object and all the waypoints, but I can't seem to figure out how to put those waypoints into that array so the enemy object moves to each one. If I drag the waypoint(clones) into the Enemy2Way(clone)s array after I push play it patrols just fine. How can I accomplish this via code?
Here is my code:
EnemyControl.cs
using UnityEngine;
using System.Collections;
public class EnemyControl : MonoBehaviour {
public static 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);
}
}
BuildLevel function in GameManager.cs
public void BuildLevel(int whatLevel)
{
if (whatLevel == 0)
{
Instantiate (Player, new Vector3 (0, 1, 7.5f), Quaternion.identity);
Instantiate (Goal, new Vector3 (0, -1.7f, 0), Quaternion.identity);
GameObject enemy1 = Instantiate (Enemy2Way, new Vector3 (0, 1, 0), Quaternion.identity) as GameObject;
enemy1.GetComponent<EnemyControl>().patrolPoints[0] = Instantiate (patrolPoint, new Vector3 (0, 1, 0),Quaternion.identity) as Transform;
enemy1.GetComponent<EnemyControl>().patrolPoints[1] = Instantiate (patrolPoint, new Vector3 (5, 1, 0),Quaternion.identity) as Transform;
}
}
I would suggest converting the patrolPoints array from Transform to Vector3 type.
Anyway, the issue I notice for the moment is that you are not instantiating (with the new operator) the array objects. That would explain why it only works with manual assigning (when you actually pass the reference to an object). I am not sure if Transforms can be instantiated and this is why I suggested Vector3 objects, besides less memory usage - transforms also hold rotation and scale and they would be pointless if you're using only positions.
$$anonymous$$y suggestion is that, if you create the positions from the code anyway, you should declare a Vector3 array, then instantiate each object separatly with new. What you did had no success because you were only creating the array; you also need to create the objects, otherwise the array would be full of null references.
Also I apologize if I was too unclear. It is pretty late here and I'm going to sleep :) good luck on your project, I hope I helped so far.
I will try the vector3 piece that you mentioned. However, I am able to instantiate the patrolpoint objects with the current code when I press play. Once play is pressed, I can drag the instantiated patrolpoint object into my patrolPoints array in the inspector and it all works the way I want. I just can't seem to figure out how to code the actual drag and drop process. If that makes any sense.
I can't really see a bug in BuildLevel. Try instantiating as GameObject and then write
[...].patrolPoints[i] = patrolPoint.GetComponent<Transform>();
Your answer
Follow this Question
Related Questions
C# SetActive GameObject Array 2 Answers
GameObjects static array NullReferenceException 1 Answer
How to modify specific GameObjects in an array of GameObjects? 1 Answer
C# Rotate GameObjects Regardless of List Size 2 Answers
C# GetComponent Issue 2 Answers