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 primus88 · May 19, 2013 at 06:58 PM · prefabspawn

Check if with a spawned gameobject

I have this line of code that destroys a child of a prefab (works) and also I want to check for a certain spawned prefab and increase my gold by 20 if it dies (does not work).

  if (ai && !ai.dead){ // if ai.dead not set yet...
     ai.dead = true; // set it and destroy capsule
     Destroy(transform.Find("Capsule").gameObject);
         if (gameObject.name == "RightGrunt(Clone)"){gold += 20;}
     }

Apparently it doesn't work because it is a spawned prefab. If I do this with the same prefab set in the scene it works. How can apply the if for the spawned prefab ?

Code is in C#

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 bdjnk · May 19, 2013 at 07:53 PM 0
Share

Where is that "gameObject" co$$anonymous$$g from? If it's the object your script is attached to, then you already know its name (unless the same script is attached to multiple kinds of prefabs, but that would be a bit silly) and you can just increment gold. I guess I'm not understanding why you're doing that name check.

avatar image primus88 · May 19, 2013 at 08:02 PM 0
Share

I have the same script for all of my prefabs because it is a general health.cs script. I decided to have the check here for gold add, because I have no idea how to do it otherwise.

avatar image bdjnk · May 19, 2013 at 08:30 PM 0
Share

Ah, I see. Well, if "RightGrunt(Clone)" is indeed the name of the Prefab Instance, which you can check in the Hierarchy tab while running, then your code should work.

$$anonymous$$aybe try with tags ins$$anonymous$$d. Add a tag to your Prefab and check for that ins$$anonymous$$d. (Adding tags is confusing if you haven't done it yet. See the Tag $$anonymous$$anager page.)

avatar image primus88 · May 20, 2013 at 08:41 AM 0
Share

I know how to work with tags. I will try this as well and return with feedback.

avatar image primus88 · May 20, 2013 at 08:51 PM 0
Share

Still not working. And I can;t manipulate the tag as it is used for another script... changing it would screw that one.

Is there a workaround? How can I check what prefab died so I can give a certain amount of gold ?

1 Reply

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

Answer by primus88 · May 22, 2013 at 08:41 AM

Anyone can shed some light on why this is happening ?

How is it possible that the code works for a prefab already in the game, but not for the same prefab that is spawned ? For days I;m thinking at this and I have no idea..

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

14 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

Related Questions

Spawn a prefab 1 Answer

Setting Position of Spawned Prefab 2 Answers

Help: script that worked don't work with Unity 3.5 0 Answers

Spawning a projectile with a trigger 1 Answer

Spawn System Troubles 0 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