• 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 Paige · Mar 31, 2011 at 07:18 PM · gameobjectobjective

Creating gameObject after items are destroyed

I'm sure this is really to do, I just can't seem to find the best way around it. I have a "gateway" at the end of my level to go to the next level, but I don't want it "opening" until after the player has destroyed some cannons.

How do I create that gameObject so it is initially there at the start of the game?

I am using javascript to do this, and was hoping for some help. Thanks for your time

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 e-bonneville · Mar 31, 2011 at 07:33 PM

Use an if statement to check if all the cannons are destroyed before opening the gateway, like this:

var totalCannonsDestroyed = 0; var destroyedCannonGoal = 5; var gatePrefab : Transform;

function Start() { gatePrefab.active = false; }

function Update() { if (totalCannonsDestroyed >= destroyedCannonGoal) { OpenGate(); totalCannonsDestroyed = 0; } }

function OpenGate() { gatePrefab.active= true; }

This script needs to go on a "LevelManager" object.

var levelToLoad = 2;

function OnTriggerEnter(other : Collider) { if (other.gameObject.CompareTag("Player")) { Application.LoadLevel(levelToLoad); } }

This script needs to go on a gate object that you want to activate when enough cannons are destroyed. It should have a collider set to Trigger on it. You'll also want some way to keep track of the next level to load. This gameObject should be disabled by default. (The little checkbox near the object's name in the Inspector panel should not be checked)

Please note that the above code is untested and may contain errors.

Where OpenGate() is the function that allows your player to access the new level. You'll want to increase totalCannonsDestroyed by 1 every time you destroy a cannon, or else your gateway will never open.

Comment
Add comment · Show 13 · 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 Paige · Mar 31, 2011 at 07:39 PM 0
Share

sorry, i don't think i cleared up perfectly what is supposed to happen. After the player destroys the cannons, i want the player to be able to go through an archway that previously wouldn't have loaded the next level. finding out if the cannons are destroyed is easy, i'm looking for something like if(...){ create.gameObject }

avatar image e-bonneville · Mar 31, 2011 at 07:45 PM 0
Share

Yes, that would be provided for in your OpenGate() function, which would theoretically do whatever you need to to create the gate. Also, I just noticed an error in the above code, which would result in an infinite loop. Let me fix that. :)

avatar image Paige · Mar 31, 2011 at 07:52 PM 0
Share

well, thats the main problem is I have no idea how to create the gameobject i have set up for the gateway after the set time. like, in the function OpenGate(), what would go in there to create said gameObject so it isn't there initially.

avatar image e-bonneville · Apr 01, 2011 at 02:07 AM 0
Share

Okay, I'll edit my answer to include the OpenGate() function for you.

avatar image Paige · Apr 01, 2011 at 09:31 PM 0
Share

"gatePrefab.enabled = true;" - that was the line i needed. didn't think of that. thanks man

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

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

Sporadic Failure of GetComponentInChildren 0 Answers

speed change in standalone compared to editor? 1 Answer

Translate.transform problems (collision/Rigibody) 0 Answers

Weird GetComponent error 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