Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ajtracy300 · Oct 15, 2014 at 03:09 PM · prefabinstantiationincorrect

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;
     }
 }














Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Instantiated PreFab selection problem 1 Answer

Accessing a game object from a script? 1 Answer

making prefabs at runtime? 2 Answers

Instantiated prefab references point to first prefab instantiated 3 Answers

Generated meshes/materials cannot be made into prefabs? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges