- Home /
Help with Prefab script running on wrong instance...
Thanks in advance to anyone who takes the time to help on this. This is my first post for assistance, so I hope I'm not breaking any rules and not posting incorrectly. I have searched Google for similar problems, but have not found any so, here it goes...
Problem:
I have a teleporter that both players and enemies can use. It activates when a player or enemy steps into the activation collider. When it activates it is supposed to enable a script on the player or enemy that entered the activation collider.
It works fine with just the player. It works fine with just 1 enemy. The problem is that when 2 enemies instantiated from the same prefab (slime) enter the activation collider at the same or different times, only the script on the first slime gets called.
This results in the script being fired on the first slime and not the second. Ever.
The teleporter script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TeleportPad : MonoBehaviour {
public float timeToReachDestination;
public TeleportPad linkedTeleporter;
public Transform arrivalZone;
public Color teleportColor;
public bool recieving = false;
void OnTriggerEnter2D(Collider2D other)
{
if (!recieving) {
string tag = other.tag;
if (tag == "Player" || tag == "Enemy") {
GameObject p = other.transform.root.gameObject;
Teleport t = p.GetComponent<Teleport> () ? p.GetComponent<Teleport> () : p.GetComponentInChildren<Teleport> ();
if (t && !t.teleporting) {
if (linkedTeleporter && arrivalZone) {
t.LinkInfo (timeToReachDestination, arrivalZone, linkedTeleporter, this, teleportColor);
t.enabled = true;
} else {
Debug.LogError ("Teleport Failed! Not all variables established for " + name);
}
} else if(t && t.teleporting)
{
t.teleporting = false;
}
}
}
}
public void Disable(float time)
{
if(!recieving)
StartCoroutine (DODisable (time));
}
IEnumerator DODisable(float time)
{
Debug.Log ("Telepad disabled");
recieving = true;
yield return new WaitForSeconds (time);
recieving = false;
Debug.Log ("Telepad enabled");
}
}
The script that is supposed to be activated on the entering player or enemy:
using UnityEngine;
using System.Collections;
public class Teleport : MonoBehaviour
{
float ETA = 3;
Transform arrival;
TeleportPad linkedPad, sendPad;
public bool teleporting = false;
Color originalColor, teleColor;
public Rigidbody2D rBody2D;
public SpriteRenderer spriteR;
public Transform transf;
public Behaviour[] disableOnTele;
// Use this for initialization
void Awake ()
{
enabled = false;
}
void OnEnable ()
{
if (!teleporting) {
if (AllVarsSet ())
StartCoroutine (TeleportSequencer ());
else
Debug.LogError ("TELEPORT FAILED! Not all vars set for " + name);
} else {
enabled = false;
}
}
public void LinkInfo(float ETA, Transform arrival, TeleportPad linkPad, TeleportPad sendPad, Color tColor)
{
this.ETA = ETA;
this.arrival = arrival;
linkedPad = linkPad;
teleColor = tColor;
this.sendPad = sendPad;
}
void ReadyPlayerForTransport ()
{
this.rBody2D.isKinematic = true;
this.originalColor = spriteR.color;
foreach (Behaviour b in this.disableOnTele) {
b.enabled = false;
}
}
IEnumerator ReadyPlayerForArrival ()
{
foreach (Behaviour b in this.disableOnTele) {
b.enabled = true;
yield return null;
}
this.rBody2D.isKinematic = false;
}
IEnumerator TeleportSequencer()
{
float timeStart = Time.time;
this.teleporting = true;
this.ReadyPlayerForTransport ();
this.StartCoroutine (Spin (ETA, 15));
yield return this.StartCoroutine (FadeToColor(1f, teleColor));
yield return this.StartCoroutine (MoveToDestination (ETA - 2f, 4));
yield return this.StartCoroutine (FadeToColor(1f, originalColor));
this.StartCoroutine(ReadyPlayerForArrival ());
this.enabled = false;
float timeEnd = Time.time;
Debug.Log ("Took " + (timeEnd - timeStart) + " long");
yield return null;
}
IEnumerator MoveToDestination(float overTime, float step)
{
float timer = overTime, x , y, veloX = 0.0f, veloY = 0.0f;
Debug.Log ("Starting to move to destination...");
while (timer > 0) {
timer -= Time.deltaTime;
x = Mathf.SmoothDamp(transf.position.x, arrival.position.x, ref veloX, overTime / step);
y = Mathf.SmoothDamp(transf.position.y, arrival.position.y, ref veloY, overTime / step);
transf.position = new Vector2(x,y);
yield return null;
}
transf.position = arrival.position;
Debug.Log ("...Ending move to destination.");
}
IEnumerator FadeToColor(float overTime, Color color)
{
Debug.Log ("Starting color change...");
float timer = overTime;
while (timer > 0) {
timer -= Time.deltaTime;
spriteR.color = Color.Lerp(spriteR.color, color, overTime * Time.deltaTime);
yield return null;
}
spriteR.color = color;
Debug.Log ("...Ending Color Change.");
}
IEnumerator Spin(float dur, float amt)
{
float timer = dur, rotation = 0.0f, newZ, velo = 0.0f, oAmt = 0, AMT =amt;
Debug.Log ("Starting Spin...");
while (timer > 0) {
if(timer > dur - 1)
{
amt = oAmt;
oAmt += .2f;
if(amt > AMT)
amt = AMT;
}
timer -= Time.deltaTime;
rotation += amt;
if(rotation > 360)
rotation = 0;
if(rotation < 0)
rotation = 360;
newZ = Mathf.SmoothDamp (rotation , rotation += amt, ref velo, amt * Time.deltaTime);
transf.rotation = Quaternion.Euler(0,0,newZ);
if(timer < 1)
{
amt -= .1f;
if(amt < 0)
{
amt = 0;
}
}
yield return null;
}
transf.rotation = Quaternion.Euler(0,0,0);
Debug.Log ("...Ending Spin.");
}
IEnumerator CooldownTimer(float time)
{
yield return new WaitForSeconds(time);
teleporting = false;
}
bool AllVarsSet()
{
return rBody2D && spriteR && transf;
}
}