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 Kensei · Nov 16, 2014 at 08:16 PM · c#addcomponentawakegame-object

AddComponent deletes the game object.

This should work right?

 void Awake()
     {
         if(Type == WeaponType.Projectile)
         {
             gameObject.AddComponent<BallisticProjectile>();
         }
     }

Instead it deletes the weapon. What am I doing wrong?

Comment
Add comment · Show 5
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 Linus · Nov 16, 2014 at 08:49 PM 0
Share

You sure this is what removes the gameobject? Could be that it replaces gameObject to become the component or a new gameobject with the component. But seems weird.

Try just: AddComponent();

avatar image Linus · Nov 16, 2014 at 08:51 PM 0
Share

Editied comment does not update for me, here is what I meant to try:

 AddComponent<BallisticProjectile>();
avatar image Kensei · Nov 16, 2014 at 10:02 PM 0
Share

There is no definition for plain AddComponent. There is AddComponent$$anonymous$$enu. You have to specify where you want that component added, in my case it's the game object that has the script. Unfortunately it disappears at runtime.

Also I'm pretty sure that this is what removes the object, since this is the only line of code that does anything.

avatar image Sir-Irk · Nov 16, 2014 at 11:30 PM 1
Share

Does your BallisticProjectile script destroy the GameObject it's on(as in Destroy(gameObject))? Could be triggering self-destruction immedialetly

avatar image Kensei · Nov 17, 2014 at 07:30 AM 0
Share

Omg yes I can't believe I missed it. It has a public float lifetime but it's not initialized by default. Thanks.

1 Reply

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

Answer by Wisearn · Nov 17, 2014 at 08:24 AM

The issue resides either in "BallisticProjectile" script or in other parts of the code not posted here.

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 Kensei · Nov 17, 2014 at 11:34 AM 0
Share

BallisticProjectile had Destroy with a public float lifetime. It didn't have a default so it was 0. Fixed it.

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

29 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to check if a script component exists before adding it? 2 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Awake not called if class has static member 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