Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by Chutney · Nov 18, 2015 at 06:42 AM · c#gameobjectinstantiateprefabweapon system

Prefab destroyed upon instantiation

Hi everybody, first question here, hope it's a good one!

I'm creating a game where the player gets multiple weapons they can swap between. To facilitate this, I have created a base class (WeaponBase) and inheriting from that, a child class (WeaponSword).

WeaponSword looks like this (pared down to relevant code only):

     public class WeaponSword : WeaponBase
     {
         // Use this for initialization
         void Start()
         {
             print("Sword Start");
         }
 
         void OnDestroy()
         {
             Debug.Log("Sword Destroyed");
         }
 
         // Update is called once per frame
         void Update()
         {
             Debug.Log("Sword Update");
         }
     }
 }

I've created a Prefab which has my sword model, animations, etc, and the WeaponSword script.

I have created a GameObject in the scene to which a script is attached to handle the weapons, and this script has a public array (weapons) which stores my list of weapons. I've dragged the Prefab into the script's weapons slot in the Unity Editor.

Here is the relevant code:

         public GameObject[] weapons;
 
         private GameObject equippedWeapon;
 
         // Use this for initialization
         void Start()
         {
             equippedWeapon = (GameObject)Instantiate(weapons[0]);
         }

     void Update()
     {
         Debug.Log(equippedWeapon);
     }

Here is where it gets tricky...

When I start the game, the weapon's model does not appear. If I pause the game and investigate the Scene view and look at the list of GameObjects, I do not see any WeaponSword objects... Looking at the console, I see some very strange behavior. The logs look like this:

 Sword Start
 WeaponSword(Clone) (UnityEngine.GameObject)
 Sword Update
 WeaponSword(Clone) (UnityEngine.GameObject)
 Sword Start
 Sword Update
 Sword Start
 Sword Destroyed
 Sword Destroyed
 Sword Destroyed
 null
 null
 null
 null

so what I gather from that is that... apparently multiple copies of this Prefab are instantiated (despite existing only as a Prefab and not in the scene initially, and the only call to instantiate is the one in the weapon management script which exists only once in the scene), then all of them are deleted (actually, all of them are deleted and then one more that does not apparently Start is Destroyed as well!), and from then on my weapon manager script stores no reference to the object.

I don't call Destroy anywhere in this code, and especially do not call Destroy on any WeaponSword objects.

Why is my instantiated object getting destroyed immediately?

Thank you for reading this rather long and involved question.

Comment
Add comment · Show 2
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 rutter · Nov 18, 2015 at 06:45 AM 0
Share

Are you familiar with the idea of a stack trace? If you expand the console window in Unity, you can click on each debug message to see the stack trace that led up to it. That should be very useful if you see the series of function calls that results in a sword getting destroyed.

I would pretty much guarantee that you are calling Destroy unexpectedly, either indirectly through some bug or from some script you didn't expect.

avatar image Chutney rutter · Nov 18, 2015 at 12:59 PM 0
Share

Hi, thank you for the response! Stack trace shows only:

Sword Destroyed UnityEngine.Debug:Log(Object) Aberon.Weapon.WeaponSword:OnDestroy() (at Assets/Scripts/WeaponSword.cs:75)

So doesn't offer too much extra information.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

What is Casting in Unity ? 1 Answer

Destroy prefabs after they have been instantiated (C#) 1 Answer

I need help about Basic Basketball Game 0 Answers

Create gameobject in other scene... 0 Answers

Find instantiated prefab by name 3 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