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 DerpimusPrime · Oct 26, 2013 at 02:16 AM · javascriptdestroy

When An Object Doesnt Exist, Help Making a Variable In Another Script Change

I need help making it so, if this object dies, set the variable "Target" on another script to the object with the tag "Player". This is the sliver of code.

 function OnTriggerEnter(other : Collider)
 {
     if(other.gameObject.tag == "Zombie")
         {
             if (BarrCooldown == false)
             {
                 health -= 1;
                 StartCooldown();
             }
         
     
             else if (health <= 0)
             {
                 Destroy (parent.gameObject);
                 other.gameObject.GetComponent(AIFollow).Target = GameObject.FindWithTag("Player").transform;
             }
         }
 }
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 robertbu · Oct 26, 2013 at 02:43 AM 1
Share

if 'parent' is the parent of this game object (the one this script is attache to), then the fix might be as simple as reversing the order of lines 14 and 15. If you are destroying the parent, you are also destroying this game object, so the script never gets to line 15.

avatar image DerpimusPrime · Oct 26, 2013 at 02:12 PM 0
Share

Did that then got this "NullReferenceException: Object reference not set to an instance of an object BarrScript.OnTriggerEnter (UnityEngine.Collider other) (at Assets/BarrScript.js:33)" I should also say, its now not destroying the parent.

1 Reply

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

Answer by meat5000 · Oct 26, 2013 at 02:15 PM

Two functions you may find useful

http://docs.unity3d.com/Documentation/ScriptReference/Transform.DetachChildren.html

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnDestroy.html

Comment
Add comment · Show 6 · 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 DerpimusPrime · Oct 26, 2013 at 02:23 PM 0
Share

If I use OnDestroy, how would I get the variable 'other' which is the collider variable, to the script that has OnDestroy on it. Should I use GetComponant?

avatar image meat5000 ♦ · Oct 26, 2013 at 02:28 PM 0
Share

Use OnDestroy to perform a final operation on an object about to be destroyed. This function goes on the destroyed object itself. It's up to you how you implement this :) If you can reference the object to destroy it you can reference it to GetComponent a variable to/from it :) $$anonymous$$aybe OnDestroy can send out a required variable before the object is destroyed.

Detach children I personally found useful for making sure child objects don't get destroyed with the parent.

avatar image DerpimusPrime · Oct 26, 2013 at 02:39 PM 0
Share

"BCE0019: 'gameObject' is not a member of 'Object'." Variable other is not defined until a trigger enters the object, no?

 function OnTriggerEnter(other : Collider)
 {
     if(other.gameObject.tag == "Zombie")
         {
             if (BarrCooldown == false)
             {
                 health -= 1;
                 StartCooldown();
             }
         
     
             else if (health <= 0)
             {
                 Destroy (parent.gameObject);
             }
         }
 }
 
 function StartCooldown()
 {
     BarrCooldown = true;
     yield WaitForSeconds(3);
     BarrCooldown = false;
 }
 
 function OnDestroy()
 {
     other.gameObject.GetComponent(AIFollow).Target = GameObject.FindWithTag("Player").transform;
 }
avatar image meat5000 ♦ · Oct 26, 2013 at 03:17 PM 0
Share

Declare a gameobject at the start of your script then in your collision function pass the other object into this variable. 'other' has limited scope as it was defined in a function but a variable declared in the class has scope class-wide.

avatar image DerpimusPrime · Oct 26, 2013 at 05:17 PM 0
Share

If I do that I get "Assets/BarrScript.js(37,18): BCE0022: Cannot convert 'UnityEngine.Collider' to 'UnityEngine.GameObject'." Because the 'other : Collider' makes other a collider variable, and if I do other = other2, and have it as "other2.GetComponent(AIFollow).Target = GameObject.FindWithTag("Player").transform;"

Show more comments

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

16 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

Related Questions

Melle system destroy game object not working despite no errors and declining health 1 Answer

simple ball rolling mechanic 0 Answers

How to create a fluid stream? 1 Answer

How do I destroy a prefab with a prefab? 1 Answer

Setting Scroll View Width GUILayout 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