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
1
Question by Davidflynn · Dec 03, 2011 at 06:06 AM · liveslife

Lives dont minus correctly

My issue is that I am using the script so that when the object i place it on (an invisable box) is collided with it respawns you back at start and takes away a life. Problem comes in that when you have 6 boxes in game with the script on each you can hit each box 3 times before you die, not 3 life in general. I want it so that if u hit any of the boxes it removes a life.

 var spawnPoint : Transform;
 //var theShadow : GameObject;
 public var life : int = 3;
 
 
 function OnTriggerEnter (other : Collider) 
 {
     other.gameObject.active = false;
     //theShadow.active = false;
     
     yield new WaitForSeconds (0.5);
     
     other.transform.position = spawnPoint.position;
     life -= 1;
     if(life == 0) Application.LoadLevel("End Game");
     
     other.gameObject.active = true;
    // theShadow.active = true;
 }
 function OnGUI(){
 
 
 GUI.Label (Rect (300, 100, 200, 30), "Life: " +life);
 }
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
2
Best Answer

Answer by syclamoth · Dec 03, 2011 at 06:12 AM

The problem here is that the box itself is holding the 'lives' value, not the player! You need to split this functionality off into two different scripts. So, on your box script, do this-

function OnTriggerEnter (other : Collider) 
{
    other.SendMessage("Die", SendMessageOptions.DontRequireReceiver);
}

Then on your player, do this-

var spawnPoint : Transform; //var theShadow : GameObject; public var life : int = 3;

function Die() { life -= 1; if(life == 0) { Application.LoadLevel("End Game"); } else { StartCoroutine(DeactivateRespawn()); } }

function DeactivateRespawn () { gameObject.active = false; yield new WaitForSeconds (0.5); transform.position = spawnPoint.position; gameObject.active = true; }

function OnGUI(){ GUI.Label (Rect (300, 100, 200, 30), "Life: " +life); }

This way, the box tells the player to die and respawn, instead of the box doing the job itself. The player should know how to do this job better than a box should.

Comment
Add comment · Show 6 · 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 Davidflynn · Dec 03, 2011 at 06:30 PM 0
Share

I guess in not doing somthing exactly correctly im geting a message BCE005: $$anonymous$$ identifier:"$$anonymous$$essageOptions". not quite sure how to fix this im a little new to scripting.

avatar image aldonaletto · Dec 03, 2011 at 08:05 PM 1
Share

The correct parameter is Send$$anonymous$$essageOptions.DontRequireReceiver (who could memorize such a long name?)

avatar image syclamoth · Dec 03, 2011 at 11:23 PM 0
Share

Oops! I made a few mistakes there. Serves me right for coding when I'm tired!

The first one, aldo found (thanks!), the second one was that in StartCoroutine, I needed the () after the DeactivateRespawan function name!

I've updated my answer with the correct code for you.

avatar image WillTAtl · Dec 03, 2011 at 11:30 PM 1
Share

"No appropriate version of 'UnityEngine.$$anonymous$$onoBehaviour.StartCoroutine' for the argument list '(function(): System.Collections.IEnumerator)' was found."

Translating this into english: The error is happening when you call "StartCoroutine," and the problem is the parameters (arguments) you passed it were not what it expected.

StartCoroutine actually takes an IEnumerator - the return value of a coroutine - as a parameter. The error messages says you're passing in a function rather than an enumerator.

Counter-intuitive as it might seem, if you're not using the string versions of coroutine (StartCoroutine("$$anonymous$$yFunctionName"), you don't pass it just the function, you actually call the coroutine function and pass the resulting IEnumerator into StartCoroutine.

A look at the line in question shows you're not calling the coroutine, just passing it's function...

 //just passing the coroutine without calling it gives this error
 StartCoroutine(DeactivateRespawn); 

What you want to do is call the coroutine as the parameter, like this

 //Calling it actually creates the coroutine and returns an IEnumerator,
 //which is passed to StartCoroutine
 StartCoroutine(DeactivateRespawn());

to help understand what's really going on, you could also write it this way:

 //calling the coroutine doesn't run it, it just CREATES it; think of it
 //as kindof like calling a constructor to make an instance of the coroutine
 var coroutineEnumerator : IEnumerator = DeactivateRespawn( );

 //StartCoroutine is what actually tells the coroutine instance you created 
 //to start running, adding it to a queue with other coroutines to run later
 StartCoroutine( coroutineEnumerator );


Unityscript's syntax really obfuscates what's going on here, so I hope this explanation helps you understand coroutines and StartCoroutine a bit better!

If that all just confused you, here's the tl;dr version: just change the line with StartCoroutine to this:

 StartCoroutine(DeactivateRespawn());
avatar image WillTAtl · Dec 05, 2011 at 04:16 AM 1
Share

I got a little crazy there, but while I'd been using coroutines for a while, I'd just a couple of days earlier had the epiphany that made the coroutine syntax actually make sense in my head, and that was the first opportunity to share that revelation with the world, haha.

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Lives bring down too fast 1 Answer

Lives aren't subtracting, going straight to game over. 2 Answers

How to slowly decrease health script? 3 Answers

Load level with lives based on score 1 Answer

How can I get my Lives scripts working 1 Answer


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