Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
5
Question by Adam 10 · Jan 28, 2011 at 09:01 PM · gameobjectinstantiateclone

Instantiated Object's Scripts not enabled? Not sure why

Don't have code right now, but I wanted to get this up in case anyone had any ideas. I will post it this evening in an edit. I have an object being InStantiated after the Enemy dies which respawns the object as a clone. Everthing looks right, but it's no longer doing anything. When I click on the object during Play mode in the Hierarchy all the scripts are unchecked. Any ideas? I do this with my player and all its scripts are enabled/checked.

@Bunny83 I will experiment with this, I think I saw something about a prefab manager in the Wiki. I was using JS, but I think I can modify this easily enough. Not entirely sure though why the player GameObject when being destroyed doesn't have this issue. I will let you know how it goes thanks.

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

3 Replies

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

Answer by Bunny83 · Jan 28, 2011 at 10:32 PM

I had that issue just a few hours ago. I posted a detailed explaination in the Unity forum.

In short: The problem is that when you assign a prefab to a variable inside that prefab, unity recognise this reference as local reference and adjust it to your real object when it gets instantiated.

That means the variable you assigned your prefab and that you use to Instantiate a new version of your object, is now pointing to the actual object in the scene and not longer to the prefab.

That alone wouldn't be such a big problem because your new object would be cloned from your old object instead from the prefab. But when you call Destroy() on your object all scripts gets disabled before it will be removed. When you instantiate a new object from the old one, all scripts are disabled.

Furthermore it causes another nasty effect. When your object is named "Enemy" the clone will be named "Enemy(clone)". The next instance will be named "Enemy(clone)(clone)". That causes endless names. That can be avoided by assigning a name after instantiate but it's all not really a good solution.

All in all you will always run into such problems when a prefab is holding a reference to itself. The best solution is to use a prefab manager script that provides easy access to this reference.

Since you didn't specified a language i will use C#:

 // This script should be an a empty gameobject somewhere in the scene
 // DontDestroyOnLoad would be great (like for the most manager)
 
 using UnityEngine;
 
 public class PrefabManager : MonoBehaviour
 {
    // Assign the prefab in the inspector
    public GameObject EnemyPrefab;
    //Singleton
    private static PrefabManager m_Instance = null;
    public static PrefabManager Instance
    {
       get
       {
          if (m_Instance == null)
          {
             m_Instance = (PrefabManager)FindObjectOfType(typeof(PrefabManager));
          }
          return m_Instance;
       }
    }
 }

It's not a perfect singleton, you could check after FindObjectOfType if we found it and in case there is no manager, create one. But it's quite useless because all the prefabs will not be assigned to it and that's the main usage of that manager.

To instantiate a new enemy from inside the enemy script can easily done with:

 Instantiate(PrefabManager.Instance.EnemyPrefab);

I'll hope that doesn't confuse you too much :D it's a really tricky problem but i don't see much better solutions. I use such a manager in our game project for nearly everything but watch out: all prefabs assigned to the manager script on the empty gameobject will be packed into the scene where the manager is placed. When you put that manager in the first scene the size of this scene will increase (but the over all size stay the same). It's just a bit nasty when you stream your game in webplayer because it can't start until the first scene is loaded.

If you have more questions on these themes just ask ;)

Comment
Add comment · Show 7 · 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 GamesDeveloper12 · Feb 15, 2014 at 03:15 PM 0
Share

the code above, gives many errors when used as a script

avatar image Bunny83 · Feb 16, 2014 at 04:19 AM 0
Share

@GamesDeveloper12:
$$anonymous$$aybe you forgot the "using UnityEngine;" at the top? I didn't included it since every C# script in Unity needs it. Anyway i added it to the code snippet above.

avatar image Vadymka · Jul 10, 2014 at 09:29 PM 0
Share

yeah id does have a lot of errors, the exact script alt text

avatar image Bunny83 · Jul 11, 2014 at 01:22 AM 1
Share

@Vadymka: Thanks, this is a quite old question (and answer). I'm not sure what happend there back then. Anyways i've fixed the singleton.

edit
Uhm i just took a look at your error messages.... The script you have errors in is called "DontDestroyOnLoad.cs" which certainly isn't the script i've posted. The script name and the class name have to match. The script i posted has to be called "Prefab$$anonymous$$anager.cs".

Also "DontDestroyOnLoad" is already a method from the Unity API so you really shouldn't use that as classname / scriptname.

avatar image CrispyArrow · Sep 11, 2015 at 09:10 AM 0
Share

Great code! so far it works flawlessly. I was wondering though, through the use of Instantiate(Prefab$$anonymous$$anager.Instance.EnemyPrefab);, can you also select the position in which it will get instantiated? I got several empty gameObjects which will serve as respawners.

So in short: is there a way to decide the position of the gameobject for when it get's instatiated?

Show more comments
avatar image
3

Answer by tendercorpse · Dec 16, 2017 at 10:04 PM

The simple solution is:

put destroy command under the instantiate command.

this way you'll create the child first, before killing the parent.

example: instantiate (gameobject,b,c) destroy.gameobject,

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 mx3307 · Oct 21, 2019 at 04:59 PM 0
Share

This fixed my issue. Thanks!

avatar image
0

Answer by klendigocci · Dec 04, 2016 at 10:47 AM

     public class ObjectScript: MonoBehaviour 
         {
             public GameObject ObjectToRespawn = null;
             public GameObject newObjToRespawn =  null;
             Vector3 pos = new Vector3(5f, -2.5f, -124f); // the fixed pos that i want my object to spawn
         
         
             void Start()
             {
         
             }
         
             void Update()
             {
                 if (transform.position.z > 50)
                 {
                     Destroy(this.gameObject);
                     createPrefab(ChallengeToRespawn);     //overload the gameobject that you want to clone
                 }
             }
         
             private void createPrefab(GameObject go)
             {
  //MoveObj and ObjDestroyer are my script files, u must replace those with ur script file names
                 (newChallenge.GetComponent("MoveObj") as MoveObj).enabled = true;   
     //this is initialize the script at runtime (Make it true or false)
                 (newChallenge.GetComponent("ObjDestroyer") as ObjDestroyer).enabled = true;
                 newChallenge = go;
                 newChallenge = Instantiate(newChallenge, pos, Quaternion.identity) as GameObject;  
     // this clone object
             }
         }

Hope that it works for you guys :)

Comment
Add comment · 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

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

9 People are following this question.

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

Related Questions

Clone Selector 1 Answer

Assign an instantiated GameObject? 1 Answer

Instantiate GameObject cloned into wrong position. 1 Answer

Reference an Instance of an Object?C# 1 Answer

Variable being shared between clones 2 Answers


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