• 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 kyrill91 · Feb 12, 2014 at 08:40 AM · prefabprefabs

Any way to control every instantiated prefab?

I want to make it so that when the player dies, all instantiated prefabs stop moving. Is there any easy way to do this?

Comment
Add comment · Show 6
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
avatar image getyour411 · Feb 12, 2014 at 08:46 AM 0
Share

Do instantiated prefabs already have some kind of hook to the player (a script reference/getComponent/etc)? I don't know what scope we are talking about by 'all' but you could put something like that in their Start(), and then it should be easy -

     if(player.isDead) {
 // do 'stop moving stff
     }

or any # of ways/approaches.

avatar image DajBuzi · Feb 12, 2014 at 11:17 AM 0
Share

I dont know if those prefabs are instantiated by the player but if not i would create some kind of prefab$$anonymous$$anager script attach it into empty game object and within this prefab$$anonymous$$anager create list of prefabs. When then player dies all prefabs in the list would be destroyed/stopped.

avatar image Scribe · Feb 12, 2014 at 11:26 AM 0
Share

when you instantiate a new object you could run something like

 var instantiatedGO = new Array();
 
 function Instantiate(go : GameObject, position : Vector3, rotation : Quaternion){
     var tempGO = Instantiate(go , position, rotation);
     instantiatedGO.Add(tempGO);
 }

then when your player dies, iterate through instantiatedGO and set them to is$$anonymous$$inematic or something.

Scribe

avatar image kyrill91 · Feb 12, 2014 at 06:41 PM 0
Share

I control the preFab spawns and movement from a script on an empty game object:

 *List<GameObject> prefabList = new List<GameObject> ();
 public GameObject prefab1;// skelesword
 public GameObject prefab2;// skelespear
 public float $$anonymous$$Time;// lowest int on random range
 public float maxTime;// hihgest int on random range
 private float timer = 0;// counts up every second
 private float spawner;// random range of time
 public bool stopPrefabSpawn = true;
 
 // Use this for initialization
 void Start () {
     spawner = Random.Range ($$anonymous$$Time, maxTime);// sets the random time range
     prefabList.Add (prefab1);// adding skelesword to list
     prefabList.Add (prefab2);// adding skelespear to list
 }
 void Update () {
     timer += Time.deltaTime;// sets timer to tick every second
     if (timer > spawner && stopPrefabSpawn) {
         int prefabIndex = UnityEngine.Random.Range(0,2);// randomly chooses 2 prefabs
         Instantiate(prefabList[prefabIndex]);// instantiates random prefab
         timer = 0;// resets timer
         spawner = Random.Range ($$anonymous$$Time, maxTime);//resets the random time range
     }
 }*

Then I have a collider on the prefab that, when colliding with player, stopPrefabSpawn = false.

avatar image kyrill91 · Feb 12, 2014 at 07:20 PM 0
Share

If I could just do:

 private BodyCollider bodyCollider;
 
 private GameObject baddies = GameObject.FindObjectsWithTag("Enemy");
 
 bodyCollider = baddies.GetComponent<BodyCollider>;

It could work... but unity doesnt like that

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by whydoidoit · Feb 13, 2014 at 05:30 AM

Use this as a base class for a script attached to the instantiated objects:

 using System.Collections.Generic;
 using UnityEngine;
 
 public class MonoCollection<T> : MonoBehaviour where T : MonoCollection<T>
 {
     public static List<T> all = new List<T>();
     
     protected virtual void OnEnableEx()
     {
     }
     
     protected virtual void OnDisableEx()
     {
     }
     
     protected  void OnEnable()
     {
         all.Add(this as T);
         OnEnableEx();
     }
     
     protected  void OnDisable()
     {
         all.Remove(this as T);
         OnDisableEx();
     }

  }

E.g.

    public class Finder : MonoCollection<Finder>
    {
    }

Now you can have a list of everything with that script available using Finder.all:

     foreach(var f in Finder.all) {
          ...
     }
Comment
Add comment · Show 1 · Share
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
avatar image whydoidoit · Feb 13, 2014 at 05:30 AM 0
Share

And it's super fast...

avatar image
0

Answer by Jellezilla · Feb 12, 2014 at 10:26 AM

how about using the GameObject.FindGameObjectsWithTag method?

So you can tag all the prefabs that are supposed to stop, with the same tag. And then when your player dies you can run the method above and make sure they all stop their movement.

Comment
Add comment · Show 1 · Share
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
avatar image Benproductions1 · Feb 12, 2014 at 10:58 AM 1
Share

Find esque functions should not be run anywhere other than the beginning of a newly loaded scene. Ins$$anonymous$$d references should be passed around when Instantiateing or similar.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Pressing Apply Breaks Prefab 1 Answer

How to find the prefab base of a Prefab Variant through code? 1 Answer

Reinitialize prefab 0 Answers

How can I infinitely generate tiles, and randomly generate prefabs on top of them with a script?,How can I infinitely generate tiles with randomized prefabs on them? 0 Answers

Object.Destroy destroying all 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