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 Velketor · Feb 11, 2012 at 04:31 PM · destroy objectload levelmouse click

Destroy Objects to Load New Scene

I have two spheres in my scene. When the player clicks on the 1st sphere, the sphere gets destroyed using the following script:

 function OnMouseDown() {

 Destroy(gameObject);

}

After the player clicks on the 2nd Sphere, I want a new level to load. I want to use the below script to load a new level when both Spheres are destroyed.

{ Application.LoadLevel("Scene_1"); } }

Any ideas? Thank you!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Posly · Feb 11, 2012 at 05:27 PM

Make a variable that hold's the number of sphere's in the game, like numberOfSpheres for example. Then everytime you destroy a sphere that value is reduced by one. When "numberOfspheres" is 0, than load the level.

Comment
Add comment · Show 4 · 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 Velketor · Feb 11, 2012 at 08:45 PM 0
Share

ok I took a stab at it on my own...I tried two methods. Neither has worked. Any thoughts? I am not a programmer so If anyone can edit the existing code I would greatly appreciate it. Thank you.

$$anonymous$$ETHOD 1: var numberOfSpheres = 2

if numberOfSpheres = 0;

{ Application.LoadLevel("Scene_1"); } }

$$anonymous$$ETHOD 2 var numberofSpheres = 2; function LoadLevel () {

 if (!numberofSpheres) = 0 {

 { Application.LoadLevel("Scene_2"); } }

}

avatar image FLASHDENMARK · Feb 11, 2012 at 09:21 PM 0
Share

Look at some basic program$$anonymous$$g syntax and you will notice something is wrong. I can give you a hit; look at you if statements.

avatar image Velketor · Feb 11, 2012 at 09:30 PM 0
Share

can anyone else help?

avatar image FLASHDENMARK · Feb 11, 2012 at 10:07 PM 0
Share

Yes, I just did. Look at some basic program$$anonymous$$g syntax and look at your if statements.

avatar image
0

Answer by theUndeadEmo · Feb 12, 2012 at 12:37 AM

you could try getting them into an array, you need to tag the spheres to "Sphere"

this is the c# code but you may need to get it to java script

 private GameObject[] spheres;
 
 void Update(){
 spheres = GameObject.FindGameObjectsWithTag ("Sphere");
 
 if(spheres==null){return; }
 
 if (spheres.Length==0){ //load new level}

}

Comment
Add comment · Show 1 · 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 Velketor · Feb 12, 2012 at 10:54 AM 0
Share

Thank you for the sample script. I tried this out but also got errors. It get an error for this line of code:

private GameObject[] spheres;

The error is: "A Namespace can only contain types and namespace declarations".

I cannot test out my scene with errors since Unity doesn't allow for it. Thank you though.

avatar image
0

Answer by Sharon Hanlon · Feb 11, 2012 at 11:26 PM

Here's one method - you might be able to do it in fewer steps/scripts.

Create an empty game object in your scene and attach the following script:

 static var sphereCount : int = 0;
 
 function Update() 
 {
     if (sphereCount == 0)
     {
         Application.LoadLevel("Scene_2");
     }
 }

Attach the script below to each of your spheres, then drag the controller you created above into each sphere's Controller property:

 var controller : Transform;
 
 function Start()
 {
     controller.GetComponent(controllerScript).sphereCount++;
 }
 
 function OnMouseDown()
 {
     controller.GetComponent(controllerScript).sphereCount--;
     Destroy(gameObject);
 }



Your error in the code above is that = assigns the value of 2, while == compares the variable to 2.

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 Velketor · Feb 12, 2012 at 10:52 AM 0
Share

Thanks for the script examples. I followed your steps but it is not working. I get a compile error. It says "$$anonymous$$ Identifier: 'Controller Script'." I cannot test the code to see if it works because Unity doesn't let you try your game out with errors.

avatar image Sharon Hanlon · Feb 12, 2012 at 08:12 PM 0
Share

Whatever you named the first script, is the name that should appear in the GetComponent statements in the second script. I called $$anonymous$$e controllerScript. And make sure you look at each sphere in the inspector and drag the empty game object into the Controller slot.

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

Load level with lives based on score 1 Answer

How do I retain the individual lighting settings when switching scenes? 0 Answers

Javascript. Respawning a character after destroyobject(). 0 Answers

destroy prefabs 1 Answer

Help with C# - destroy at a certain position not working 0 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