Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can't GameObject.FindWithTag of instantiated gameobject

Discussion in 'Scripting' started by Slaghton, Apr 24, 2014.

  1. Slaghton

    Slaghton

    Joined:
    Sep 9, 2013
    Posts:
    139
    So I have a script set up so that if a tagged prefab gets within a certain distance of another certain gameobject it renders the object yellow/green. It works fine for objects already created before runtime but during runtime it won't interact with the cloned/instantiated gameobject.

    If anyone can help figure this out i would greatly appreciate it. I have some idea's and i tried acouple but I'm still pretty new to all this.

    Here's the code: This works for un-instantiated gameobjects. Cloned objects give errors/null references. Perhaps if i set the instantiated prefab in the script = something and reference it i could get something to work. I'm still learning the syntax.

    Code (csharp):
    1.  
    2. var Distance;
    3. var useDistance = 2.0;
    4. var target : Transform;
    5.  
    6.  
    7. function Start () {
    8. target = GameObject.FindWithTag("Player").transform;
    9. }
    10.  
    11. function Update ()
    12. {
    13.     useDistance = Vector3.Distance(target.position, transform.position);
    14.    
    15.     if (Distance < useDistance)
    16.     {
    17.         renderer.material.color = Color.yellow;
    18.     }
    19.    
    20.    
    21.     if (Distance > useDistance)
    22.     {
    23.         renderer.material.color = Color.green;
    24.     }
    25.  
    26.  
     
    Last edited: Apr 24, 2014
  2. chrisall76

    chrisall76

    Joined:
    May 19, 2012
    Posts:
    667
    I would suggest instead of using Find, you use OnTriggerEnter and OnTriggerExit which picks up only objects tagged with "Player".
    Sorry though, I'm a C# person.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class EnemySight : MonoBehaviour {
    6.  
    7.     public float WaitUntilReset = 0;
    8.     public string TagToNotice = "Player";
    9.  
    10.     public bool NoticeTag = true;
    11.  
    12.     void OnTriggerEnter(Collider other) {
    13.     if (other.gameObject.tag == TagToNotice) {
    14.         renderer.material.color = Color.yellow;
    15.         }
    16.     }
    17.  
    18.     void OnTriggerExit(Collider other) {
    19.         if (NoticeTag) {
    20.             if (other.gameObject.tag == TagToNotice) {
    21.                 renderer.material.color = Color.green;
    22.         }
    23.         }
    24.     }
    25. }
    26.  
     
  3. recon0303

    recon0303

    Joined:
    Apr 20, 2014
    Posts:
    1,634
    It made a clone of it when you do that, you need to
    Code (csharp):
    1. if(col.name == "Player(Clone)")  
    2.  
    3.  
    So if it was a normal player it would be named just Player. This is just an example, this is why it returned null because its not the original , its a clone so have it look for the clone of it, also the user above me works as well. If you need it in Java script let me know, I work with both of them.
     
    Last edited: Apr 24, 2014
  4. Slaghton

    Slaghton

    Joined:
    Sep 9, 2013
    Posts:
    139
    Thanks for the replys/solutions! I'll make sure to try both out for learning in the morning. I'm spent after getting a different script to finally work.

    (p.s. I'm a js guy but I should be able to convert it over =)