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 Highway62 · Aug 10, 2012 at 12:21 AM · instantiateobjectinstance

How do I restrict game to only instantiate 1 instance of object?

I'm building a space shooter (i know, but it's my first ever game). I have an asteroid in my game that when it is hit by a bullet and explodes, it leaves behind a shield 'token' that the player can collect and it activates their shield. The problem is, my ship has two guns, which fire simultaneously, so if the player hits the asteroid with both bullets, 2 instances of the shield token are instantiated.

Is there any way to restrict this so that only one shield 'token' is instantiated? I tried a boolean flag with an IF statement but it didn't seem to work, I also tried turning the bullet collider off once the bullet hits the asteroid, anyone have any ideas?

my code:

//script for bullet

var explosion :Transform; var shieldBut :Transform;

if(other.gameObject.tag == "asteroidShield") { //create impact explosion Instantiate(explosion, transform.position, transform.rotation); audio.PlayClipAtPoint(fxExplosion,transform.position); Destroy(gameObject); //instantiate shield 'token' Instantiate(shieldBut, transform.position, transform.rotation); }

Any help would be greatly appreciated.

Comment
Add comment · Show 1
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 Highway62 · Aug 09, 2012 at 09:05 PM 0
Share

Don't know why this question hasn't been approved by the mods yet, but I found a solution: I simply instantiated the shield token from the asteroid, ins$$anonymous$$d of instantiating it from the bullet.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by riggerman · Aug 10, 2012 at 11:55 AM

Yeah, actually you'll see further on that it's better to have the instancing of your "powerups" centralized in a static class or even into a separate prefab. That way it is easier and cleaner to keep track of what has been instantiated. (think farther, in counts for achievements)

Then, no matter how much bullets hit the asteroid, the centralized spawner would filter and let only one shield token be instantiated. :)

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to instantiate object onto other object from script 1 Answer

Instantiate a COMPLETELY Unique Instance of an Object 1 Answer

NullReference Exception when adding a class 2 Answers

Associate objects to a prefab 1 Answer

Random array issue C# 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