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 Infectedspleen · Jan 05, 2014 at 09:07 AM · destroyobjectslevelexit

When all Coins are collected, an exit level object appears.

Hi all,

I'm making a simple platform game for my son and so far I've managed to solve most problems by reading forums etc etc.

I'm my game the player has to collect coin like objects then head over to the exit object where the player progresses to the next level.

So far everything works. Coins destroy when the player touches them and updates the score, player can exit the level when they touch the exit object. But I've found when I ask my son to test the game, some times he skips collecting the coins and goes for the exit object. Thus making the game to simple and short.

My question to all is how would I go about implementing a system where all the coins on the particular level must be collected in order for the exit object to appear in the level in a given place.

Any help to point me in the right direction in regards to reference material or web links, or snippets of script would be great.

Many thanks in advance

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 Benproductions1 · Jan 05, 2014 at 09:09 AM 0
Share

Just add 1 to a counter every time you destroy a coin, then add a condition before the level is exited. If you don't know how to do that, you should look up how to code and then come back here :)

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by HappyMoo · Jan 05, 2014 at 01:02 PM

Hi,

I have a few suggestions.

You could count your coins manually and then add a check for the player score in the exit object, but if you have multiple levels, you would need to keep track of a running sum etc... there's a better way:

Have every coin in the level report itself to the exit object

 function Start()
 {
   var theExit = GameObject.FindWithTag("Exit").GetComponent(Exit);
   theExit.registerCoin();
 }

and also report to the Exit when you pickup a coin ...

 var theExit = GameObject.FindWithTag("Exit").GetComponent(Exit);
 theExit.coinPickedUp();


Then in the exit, keep track of how many coins registered and got picked up

 var coinsAlive = 0;
 function registerCoin()
 {
   coinsAlive++;
 }
 function coinPickedUp()
 {
   coinsAlive--;
 }

and check for that value before you leave the level

 function OnTriggerEnter (other : Collider)
 {
     if(other.tag == "Player" && coinsAlive==0)
     {
        audio.PlayOneShot (coinSound);
        yield WaitForSeconds (0.9);
        Application.LoadLevel (1);
  
     }
 }


To make it visually understandable, I would recommend that the Exit changes appearance. Hide some or all visual components in Start() and in coinPickup() check if you reach 0 and if so, make it visible, play your exit activation sound etc.

Regarding the level design, I would recommend to not make all coins needed to pick up, but have normal coins that are optional and some master coins(and only those report to exit) in another color that you have to pick up. Then place the master coins in a way that leads you to most of the normal coins, but still allows you to skip some normal coins.

Also, let your son only playtest the newest level and not too often, then when the game is done, he still finds every level interesting and hasn't played every one of them - especially the first ones - uncountable times.

Comment
Add comment · Show 17 · 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 Infectedspleen · Jan 05, 2014 at 01:07 PM 0
Share

Thank you for this. Time for a cup of $$anonymous$$ and lock my self in the office. I will report back with my findings. Thank you

avatar image HappyMoo · Jan 05, 2014 at 01:11 PM 0
Share

Another thing... put a public variable "nextLevel" on your exits, so you can string your levels together without additional checks in your code

avatar image Infectedspleen · Jan 05, 2014 at 01:30 PM 0
Share

Done. Handy tip. I'll have to sit done with some reference. as i get errors. I'm probably being dumb but is the above c#.

avatar image HappyMoo · Jan 05, 2014 at 01:37 PM 0
Share

I usually do C#, but seeing you use Javascript, I tried to also write Javascript, but slipped one time.. void should be function. Fixing it above. $$anonymous$$ore errors?

avatar image Infectedspleen · Jan 05, 2014 at 01:52 PM 0
Share

This is cool. I only get two errors now. i think it has something to do with the tag.

Assets/Scripts/CoinReport.js(5,64): BCE0005: $$anonymous$$ identifier: 'Exit'. Assets/Scripts/LoadLevel.js(4,62): BCE0005: $$anonymous$$ identifier: 'Exit'.

$$anonymous$$y Exit object has a tag set to Exit.

Here is my Script which is attached to each Coin...

 #pragma strict
 
 function Start()
 {
   var CoinReport = GameObject.FindWithTag("Exit").GetComponent(Exit);
   CoinReport.registerCoin();
 }

And here is the LoadLevel Script which is attached to the Exit Object...

 #pragma strict
 
 var exitSound : AudioClip;
 var CoinReport = GameObject.FindWithTag("Exit").GetComponent(Exit);
 CoinReport.coinPickedUp();
 
 var coinsAlive = 0;
 function registerCoin()
 {
   coinsAlive++;
 }
 function coinPickedUp()
 {
   coinsAlive--;
 }
 
 function OnTriggerEnter (other : Collider)
 {
     if(other.tag == "Player" && coinsAlive==0)
     {
        audio.PlayOneShot (exitSound);
        yield WaitForSeconds (0.9);
        Application.LoadLevel (1);
  
     }
 }
Show more comments
avatar image
1

Answer by superluigi · Jan 05, 2014 at 09:28 AM

There are too many different ways to go about doing this. For example you can add a boolean to your exit object and call it "on" or "activate" or whatever you want. The boolean is of course set to start as false and you can code it to where it becomes true when the score = the total amount of coins. If you tell me how you are managing your score I can help you write the code, as it is I don't know if you're handling the score through your character script, through a scene script, etc.

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 Infectedspleen · Jan 05, 2014 at 09:35 AM 0
Share

Thanks for the reply. This is very helpful. I will post my script that destroys and adds score in a short while. Got to go and do dad things before I can lock my self in my office for the day.

avatar image Infectedspleen · Jan 05, 2014 at 11:34 AM 0
Share

Hi there again.

This is the script that i attach to my Player which sets the score and destroys the Coins....

 #pragma strict
 
 private var score : int = 0;
 var guiScore : GUIText;
 var coinSound : AudioClip;
 
 function Start () {
     guiScore.text = "Score: 0";
 }
  
 function OnTriggerEnter( other : Collider ) {
     if (other.tag == "Coin") {
         audio.PlayOneShot (coinSound);
         yield WaitForSeconds (0.3);
         score += 5;
         Destroy(other.gameObject);
         guiScore.text = "Score: " + score;
     }
 }


And this script is attached to my exit object...

 #pragma strict
 
 var coinSound : AudioClip;
 
 function OnTriggerEnter (other : Collider)
 {
     if(other.tag == "Player")
     {
         audio.PlayOneShot (coinSound);
         yield WaitForSeconds (0.9);
         Application.LoadLevel (1);
         
     }
 }


I Think i understand what you mean with the use of a boolean. Thank you

avatar image
-2

Answer by coolfireking2 · Jan 05, 2014 at 09:43 AM

Hello,

You will want to use Instantiate e.g.

 var prefab : Transform;
 
 function Update(){
 
 if(coinsCollectedVar == 50) { //How many coins you need
 
 Instantiate (prefab, Vector3(0,0,0)); //position of where the object is spawned
 
 }
 
 }

Then set the prefab var to your exit game object.

Please note this code is untested but i think it will work :)

Thanks, Joseph

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 Benproductions1 · Jan 05, 2014 at 09:51 AM 0
Share

This doesn't answer the question. The question asked how to count not how to "exit"

avatar image
0

Answer by OrbitSoft · Jan 05, 2014 at 09:31 AM

You'll need a general script where you'll hold the collected coins count, let's say it will be the script attached to your exit gameObj(I will call it ExitObj).

So, ExitObj needs to count how many coins the player collected:

 public class Coin : MonoBehaviour
 {
    void Awake()
    {
    ExitObj.coinsInLevel++;
    }
 }

By adding this function to your coin script you will keep count of how many coins are in the level. Before destroying the coin you need to add collectedCoins by 1 like this:

 ExitObj.collectedCoins++;
 Destroy(this.gameObject);


And add this to your ExitObj.

 public class ExitObj : MonoBehaviour
 {
     public static int coinsInLevel;
     public static int collectedCoins;
 
     void Update()
     {
       if (collectedCoins == coinsInLevel && Input.GetKeyDown("somekey"))
       {
       // exit level
       }
     }
 }


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

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

Related Questions

Cannot touch cloned prefabs 0 Answers

Why isn't this script working? (Beginner in coding) 2 Answers

Can I utilize LoadLevelAdditive for immersive game? 2 Answers

exit level and return to main menu 3 Answers

Destroy vs Don't Destroy 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