- Home /
How do I acces a specific GameObject within the terrain????
How do I acces a specific GameObject within the terrain????
I had developed a small terrain that spawning a number of enemys in random locations. However the clones that are spawned have Missing (GameObject)
However my Enemy script its attached below. Note that the script its working fine when the enemy is in the Hierarchy as an 'Object' instead of a clone:
public GameObject target;
public Animator animator;
public AudioSource attackAudio;
// Use this for initialization
void Start()
{
animator = GetComponent<Animator>(); //Default Animator IDLE
attackAudio.Stop();
}
// Update is called once per frame
void Update()
{
. . .
}
What should i set to my to my for loop to get a specific GameObject??
I'm pretty sure you just never assign target. When you spawn new tiger, you should assign target in TigerAI or maybe find your target in Start() method of TigerAI and assign it.
Isn't that the way that I assign a new target?? (Spawn_Object_Script : Line 43) or do you suggest to move that code, or the one that has to be done, to the TigerAI??
What you do in line 43 is just changing tag of target object. I think at that point target is null
so you also get Exception. But even if you didn't get an exception, you would only change tag of target which is not the same as assigning target. What is your target? Do you have reference to it somewhere? If it exists in scene when tiger is created, you can find it with FindObjectOfType<PlayerAI>()
. Or if you have reference to it in spawning script, you can directly assign it with GetComponent<TigerAI>().target = player
. player
is reference to target, of course.
I used this and its working fine i think
public GameObject CloneTarget; //Attach a new prefab (same player prefab) in the
. . . .
for (int i = 0; i < TigerGroup.Length; i++) {
Vector3 position = new Vector3(Random.Range(300, 600), 150, Random.Range(235, 710));
TigerGroup[i] = Instantiate(Original_tigerPrefab, position, Quaternion.identity) as GameObject;
TigerGroup[i].GetComponent<TigerAI>().target = CloneTarget;
}
Let me know if there is more efficient way that this. Thanks in advance.
Prefabs are designed to be scene independent. Prefab object scripts should not have reference to scene objects or components since the link will be broken, but can have reference to its own child object or components. Try getting reference to the target (assu$$anonymous$$g its the player) dynamically using the script. For instance, in the enemy script just near to getting the animator component.
// Use this for initialization
void Start()
{
//Get reference to the player object by name
target = GameObject.Find("Player");
//CONTINUE CODE
animator = GetComponent<Animator>(); //Default Animator IDLE
attackAudio.Stop();
}
Your answer
Follow this Question
Related Questions
How do I acces a specific GameObject within the terrain???? 1 Answer
Array values changing after gameObject is instantiated from prefab 1 Answer
How does instantiate connect to rigidbody?,How does instantiate userigidbody without getcomponent? 1 Answer
Array with several gameobjects and their respective script 0 Answers
For loop and instantiate question 0 Answers