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.
var Distance;
var useDistance = 2.0;
var target : Transform;
function Start () {
target = GameObject.FindWithTag("Player").transform;
}
function Update ()
{
useDistance = Vector3.Distance(target.position, transform.position);
if (Distance < useDistance)
{
renderer.material.color = Color.yellow;
}
if (Distance > useDistance)
{
renderer.material.color = Color.green;
}
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.
using UnityEngine;
using System.Collections;
public class EnemySight : MonoBehaviour {
public float WaitUntilReset = 0;
public string TagToNotice = "Player";
public bool NoticeTag = true;
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == TagToNotice) {
renderer.material.color = Color.yellow;
}
}
void OnTriggerExit(Collider other) {
if (NoticeTag) {
if (other.gameObject.tag == TagToNotice) {
renderer.material.color = Color.green;
}
}
}
}
It made a clone of it when you do that, you need to
if(col.name == "Player(Clone)")
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.
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 =)