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 Ouija · Feb 22, 2014 at 09:34 PM · destroyactive

Active game object before destroying another

Hey guys, I'm using JavaScript for this one. In my script I am using

Destroy (gameObject);

But before the script destroys the objects that it is on, I wanted it to enable another gameobject in the scene.

I can post the script I am using if you like, but it seems pointless? Just know the script I\m using destroys itself, I am wondering where and what I would add to enable another game object in the scene before it destroys. Any ideas? :) hope that wasn't worded weirdly lol

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by jmparavicini · Feb 22, 2014 at 10:31 PM

Im not very sure but that can maybe help var (yourgameobject) : GameObject;

 function Update ()
 {
     if(input.GetKeyDown(Keycode.Space))
     {
         (yourgameobject).active = true;
         Destroy(gameObject);
     }
 }
 
Comment
Add comment · Show 2 · 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 Ouija · Feb 22, 2014 at 11:19 PM 0
Share

Hey man, it works! thanks a bunchhhh

avatar image jmparavicini · Feb 23, 2014 at 10:19 PM 0
Share

no problem :D

avatar image
1

Answer by robertbu · Feb 22, 2014 at 09:39 PM

If it is destroying itself, why not the line before:

 GameObject.Find("SomeOtherSceneObject").SetActive(true);
 Destroy(gameObject);

Alternately you can use the OnDestory() callback:

https://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 Ouija · Feb 22, 2014 at 10:19 PM 0
Share

I applied that part of script to my script and it said, object reference not set. Where should this be applied? I keep getting the same error when I tried sometihng similar myself, I must be doing something stupid lol

Edit: "someotherscene object" is that the name of another object in the scene just as it says? or should that be a tag or sorts? The object I want activated is off by its lonesome

Edit: Just now I applied the code, and I change it from true to False. And it turned off the object ins$$anonymous$$d, that worked. SO I think the issue is, the object still destroys itself before it can make the gameobject true.

avatar image robertbu · Feb 22, 2014 at 10:43 PM 0
Share

'SomeOtherSceneObject' is the name of the object that is 'by its lonesome'. You could do it by tag using GameObject.FindWithTag(). Put a Debug.Log() statement between these two lines.

 GameObject.Find("SomeOtherSceneObject").SetActive(true);
 Debug.Log("Got to here");
 Destroy(gameObject);

If the 'Got to here' fires, then the object should have been activated. Note the 'Find()' find the first object in the scene with that name. If the object you are about to delete has the same name, it may be finding the current object ins$$anonymous$$d. You can get around that problem (if that is the issue) by changing the name of the current game object.

avatar image Ouija · Feb 22, 2014 at 11:00 PM 0
Share

What is interesting is, if I set it to (false), it will make it to the dedug log. But when true it says no reference

avatar image robertbu · Feb 22, 2014 at 11:20 PM 0
Share

Oh, stupid me. GameObject.Find() only finds active objects. So this line:

 GameObject.Find("devil_flame").SetActive(true);

...will be producing a null reference exception if the object is disabled. Here are some choices:

  • Create a public GameObject variable reference in the top of this file and initialize it by dragging and dropping 'devil_flame' onto the variable. Then you can use this variable to activate the object.

  • Start with 'devil_flame' active. In Start() use GameObject.Find() to find the object, save the reference then disable it.

  • Don't disable 'devil_flame'. Hide it ins$$anonymous$$d by turning off the render and any other components that need to be disabled. This script would then enable those components.

And there are a few other more obscure solutions.

If both objects exist in the scene at start, the first choice is easiest. At the top of the file put:

 var devilFlame : GameObject;

Initialize this variable by dragging the 'devil_flame' game object onto this variable. In the code you can enable it by:

 devilFlame.SetActive(true);

  
avatar image Ouija · Feb 22, 2014 at 11:41 PM 0
Share

Thank you good sir! Your answer had been given thumbs up lol, if it doesn't show let me know, it tends to do that time to time!

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

20 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

Related Questions

About Optimization, Garbage Collection, Unused Scripts. 1 Answer

MissingReferenceException 1 Answer

Instantiate problem 1 Answer

What is the difference between these three ways of making an object disappear? 3 Answers

How To Keep GameObjects From Enabling Again? 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