Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Vice_Versa · Jul 26, 2015 at 01:02 AM · gameobjectinstantiatedestroy

How to instantiate a prefab after its been destroyed

So heres what im trying to do. I want to have a prefab instantiated on a collision, appear for two seconds, then be destoyed. This will get called repeated times. After the first time, i get the error that the gameObject trying to be instantiated has already been Destroyed. I tried making a temporary gameobject to be instantiated and destroyed, but the call to destroy is in a different method (and needs to be since their being called from a seperate script) so i cant reference that temporary gameObject. I know i figured this out before and can be done, but i forget how i did it. heres my code using UnityEngine; using System.Collections;

 public class changeCursor : MonoBehaviour {
     double t;
     float tf;
     public GameObject cursorAnimation;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     public void wait(float x){
         t = Time.time; 
         tf = Time.time + x;
         
         while(t < tf){
             t = t + 0.001; 
         }
         
     }
 
     public void playAnimation(){
         GameObject cursorInstance;
         cursorInstance = (GameObject) Instantiate (cursorAnimation, transform.position, transform.rotation) as GameObject;
     }
 
     public void hideCursor(){
         gameObject.GetComponent<Renderer> ().enabled = false;
     }
     public void goBackNormal(){
         Destroy (cursorInstance);
         gameObject.GetComponent<Renderer> ().enabled = true;
     }
 }
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

1 Reply

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

Answer by Alec-Slayden · Jul 26, 2015 at 01:14 AM

The code you posted shouldn't compile, and you shouldn't even get to play mode: cursorInstance is only defined in the scope of playAnimation(), but you are using it in goBackNormal(). I assume this is code that was intended to fix your problem but didn't work yet. Close though! you're only one step away :)

define cursorInstance as a private variable at the top of your script, and remove its declaration from playAnimation(). That way you can use it in other methods, including where it needs to be destroyed.

EDIT: As a side note, your wait method isn't a coroutine, and so probably won't do what you expect. As it stands now, it will set “t” equal to “tf” in one frame, because the while loop will keep working until it’s finished. Using a coroutine you can yield until the next frame. I am not sure what you are trying to do with it, or i would provide some examples, but I recommend looking into coroutines.

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 Vice_Versa · Jul 26, 2015 at 01:43 AM 0
Share

this was the second code, it doesnt compile. the first one did but would give me that error. This is working. The problem im having now is the Destroy call is just not working now for some weird reason

avatar image Alec-Slayden · Jul 26, 2015 at 01:51 AM 0
Share

I'd have to see the code that calls the methods to help with that unfortunately.

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

21 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Guided Missiles help? 2 Answers

PlayerRespawn class wont Instantiate the player prefab 1 Answer

Why won't this object destroy? 2 Answers

How to check if a game object is destroyed from array? And instantiate a new game object when a game object is destroyed? 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