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 SpeedTutor · Oct 19, 2013 at 08:51 PM · javascriptinstantiatedestroy

Instantiating and destroying object

Hello,

I'm wondering if someone can point me in the right direction of how to destroy my instantiated object, I looked at some other posts and this seemed to solve others but not for me. Maybe I'm doing something wrong.

 #pragma strict
 
 var player : Transform;
 var bubbleshield : Transform;
 
 private var canSpawn : boolean = true;
 
 var timeUntilNextSpawn : float;
 
 function Update () 
 {
     if(canSpawn == true && Input.GetKeyDown("b"))
     {
         var cloneBubble = Instantiate (bubbleshield, player.transform.position + Vector3(0, 10, 0), Quaternion.identity);
         canSpawn = false;
     }
     
     if(canSpawn == false)
     {
         timeUntilNextSpawn += Time.deltaTime;
     }
     
     if(timeUntilNextSpawn >= 5)
     {
         canSpawn = true;
         Destroy(cloneBubble);
     }
 }

As a side, my instantiated prefab spawns on it's side as if the rotation is off. Yet when I drag the prefab into the scene, it's fine. Not really sure if I can resolve that issue.

Cheers!

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
2
Best Answer

Answer by robertbu · Oct 19, 2013 at 09:06 PM

I'm not sure of your issue since you did not describe your problem, but typically I do my Instantiate() this way:

 var cloneBubble = Instantiate (bubbleshield, player.transform.position + Vector3(0, 10, 0), Quaternion.identity) as GameObject;

'as GameObject' specifies the type so that cloneBubble get the right type. Also you are not setting timeUntilNextSpawn to 0.0 when you instantiate a new game object. This means that timeUntilNextSpawn will always be grater than 5.0 after the first time this code is run, which means that your object wil lbe destroyed right after it is created.

As for the rotation issue, I suspect that your prefab has a rotation other than (0,0,0). Your Instantiate() is forcing a rotation of (0,0,0) when you use 'Quaternion.identity'. You can handle it like this:

 var cloneBubble = Instantiate(bubbleshield) as GameObject;
 cloneBubble.transform.position = player.transform.position + Vector3(0, 10, 0);

This will preserve the rotation of the prefab but set the position.

Comment
Add comment · Show 7 · 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 SpeedTutor · Oct 19, 2013 at 09:47 PM 0
Share

That sorted my rotation issues, thanks. Although the destroying is still amiss, it's now throwing up a "Null reference" error when I press the valid key.

Could I also do:

 cloneBubble.transform.position = player.transform.position + Vector3(0, 10, 0); //To spawn above the player?
avatar image robertbu · Oct 19, 2013 at 10:55 PM 0
Share

Yes, you can do the line you describe, and I've edited my answer since that is what it should be. So what does your code look like now, and on what line is the null reference exception? If you make bubbleShield a GameObject ins$$anonymous$$d of a Transform does the problem go away?

avatar image SpeedTutor · Oct 19, 2013 at 11:14 PM 0
Share

$$anonymous$$y code, looks the same apart from I used this ins$$anonymous$$d of what I had:

     var cloneBubble = Instantiate(bubbleshield) as GameObject;
     cloneBubble.transform.position = player.transform.position + Vector3(0, 10, 0);

I tried changing the Transform to GameObject, but brought this up:

 InvalidCastException: Cannot cast from source type to destination type.

As for the Null Reference, that is called when line 15 comes into play, so:

 cloneBubble.transform.position = player.transform.position + Vector3(0, 10, 0);
avatar image robertbu · Oct 19, 2013 at 11:25 PM 1
Share

In taking a close look I found a couple more problems:

You were declaring 'cloneBubble' with local scope, so it was not available in the Destory(). Also you have 'player' as a transform, but are calling 'player.transform.positon'. I think the following is what you want. I made a few other, not bug changes:

 #pragma strict
  
 var player : Transform;
 var bubbleshield : GameObject;
 private var cloneBubble : GameObject;
 private var canSpawn : boolean = true;
 private var timeUntilNextSpawn : float;
  
 function Update () 
 {
     if(canSpawn && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.B))
     {
         cloneBubble = Instantiate(bubbleshield);
         cloneBubble.transform.position = player.position + Vector3(0,10,0);
         canSpawn = false;
         timeUntilNextSpawn = 0.0;
     }
  
     if(!canSpawn)
     {
        timeUntilNextSpawn += Time.deltaTime;
     }
  
     if(timeUntilNextSpawn >= 5)
     {
        canSpawn = true;
        Destroy(cloneBubble);
     }
 }
avatar image SpeedTutor · Oct 19, 2013 at 11:36 PM 0
Share

That looks more promising! Thanks for taking the time to look and help out. Ill try and run this script tomorrow as I'm not at the PC. I will be sure to vote up and mark as answered when done. All the best.

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

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

Instantiate prefabs just before it comes into view 2 Answers

Instantiate JS error that i can't figure out 1 Answer

Instantiate prefabs before it comes into view 0 Answers

removing an instantiated prefab 1 Answer

Check if child exists and instantiate as child 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