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 pdoransdesign · Mar 18, 2014 at 09:21 AM · gameobjectscene-loadingcollectionslevelload

Load a new level after objects have been collected

Hi,

I am making a simple game and I am a unity beginner. In my game the player must go around the maze and collect coins to get a score. So far this part is working. I have a chest in the level and when the player reaches the chest they move up to the next level. However, I would like to make sure the player has collected all the coins before the chest moves the player to the next level. Otherwise the player can skip collecting the coins and go straight to the next level.

So far I have this script attached to my player which is counting the collected coins:

 var score = 0; 
 var guiScore : GUIText; 
 var nextLevel : boolean; 
 
 function Start (){ 
    guiScore.text = "Score : 0"; 
 } 
 function OnTriggerEnter(other : Collider){ 
    Debug.Log("OnTriggerEnter() was called"); 
    if (other.tag == "Coin"); { 
    score += 5; 
    guiScore.text = "Score: " + score; 
    Debug.Log("Score is now " + score);
    Destroy(other.gameObject); 
 
  } 
 } 
 function AddScore(){ 
 score++; if( score == 20 ){ Application.LoadLevel(2); } }

Then this is the script attached the the chest:

 public class : MonoBehaviour 
 { 
    void Awake() { 
    ExitObj.consInLevel++; 
   function OnMouseDown (){ 
   Application.LoadLevel(2); 
 }

I have no idea what I'm doing. Please help.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Yofurioso · Mar 18, 2014 at 12:52 PM

You should have an int variable on the player script to keep track of how many coins he has picked up, another int value on the chest script to recieve the coins from the player and if statement to check if the amount of coins is right:

 var score = 0; 
 var guiScore : GUIText; 
 var nextLevel : boolean; 
 
 var coinsCollecter : int;
  
 function Start (){ 
    guiScore.text = "Score : 0"; 
 } 
 function OnTriggerEnter(other : Collider){ 
    Debug.Log("OnTriggerEnter() was called"); 
    if (other.tag == "Coin"); { 
    score += 5;
    coinsCollected ++;
    guiScore.text = "Score: " + score; 
    Debug.Log("Score is now " + score);
    Destroy(other.gameObject); 
  
  } 
 } 

chest script:

 public class : MonoBehaviour 
 { 
    public int coinsInChest = 0;
 
    void Awake()
    { 
       ExitObj.consInLevel++; 
    }
 
    public void OnMouseDown()
    {
       coinsInChest += GameObject.FindGameObjectWithTag("Player").coinsCollected;
      GameObject.FindGameObjectWithTag("Player").coinsCollected = 0;
 
       if(coinsInChest >= 100) //put the amount you want here
       {
          Application.LoadLevel(2);
       }
    }
 }

By the way, I've noticed that the first script is js and the second is c#, you may want to keep your scripts on the same ¿language?. Hope it helps

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
avatar image
0

Answer by TheMannyzaur · Mar 18, 2017 at 01:46 PM

how about u create a bool to check if everything is collected. once it is you activate the chest. get it?

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

22 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 avatar image

Related Questions

Asset Bundle starts loaded 0 Answers

Loading a level and getting all new gameobjects 1 Answer

How to destroy a gameobject on collision and keep it dead on reload of the scene? 1 Answer

survival shooter game restarting when player dies 2 Answers

How do I stop the scene from switching when the gameObject is destroy / 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