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 majormetallica · Jul 15, 2014 at 09:43 PM ·

problem with my simple script please help.

here is my script

 using UnityEngine;
 using System.Collections;
 
 public class countdowntimer : MonoBehaviour 
 {
     public float timer;
     public float timerLimit = 30.0f;
 
     public GUIText timerText;
     // Use this for initialization
     void Start () 
     {
         timer = timerLimit;
         SetTimerText ();
     }
     
     // Update is called once per frame
     void Update () 
     {
         timer -= Time.deltaTime;
         SetTimerText ();
 
         if(timer <= 0.0f) 
         {
             print ("You Lose!");
             timer = timerLimit;
 
         }
     }
 
     void SetTimerText()
     {
         timerText.text = "Time Left: " + timer .ToString("f0");
     }
 }




the problem is it wont print the message in this case YOU LOSE! i have the same problem with my win script aswell. everything else works it just wont print when the float is equal to or less then 0.0f. what is going on?

Comment
Add comment · Show 9
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 rutter · Jul 15, 2014 at 08:49 PM 0
Share

Have you checked your console window for any errors or warnings?

Are you absolutely sure that this script is attached to something in the scene?

If I comment out the internals of SetTimerText, and set timerLimit to just 3 seconds, I end up seeing the message shortly after I start the scene.

avatar image majormetallica · Jul 15, 2014 at 09:09 PM 0
Share

there are no errors or warning in the console. and the script is attatched to the empty game object as a script guitext

avatar image majormetallica · Jul 15, 2014 at 09:09 PM 0
Share

and it will countdown from whatever i set it to but when it gets to zero it wont print the message

avatar image majormetallica · Jul 15, 2014 at 09:16 PM 0
Share

it also wont print the win text here is the script

using UnityEngine; using System.Collections;

public class FinishLine : $$anonymous$$onoBehaviour { public GUIText WinLoseText;

 // Use this for initialization
 void Start () 
 {
     WinLoseText.text = "";
 }
 
 // Update is called once per frame
 void Update () 
 {
 
 }

 void OnTriggerEnter(Collider other)
 {
     if (other.tag == "boulder") 
     {
         //print ("You Win! :)");
         WinLoseText.text = "YOU WIN :)";
     }
 }

}

avatar image $$anonymous$$ · Jul 15, 2014 at 09:55 PM 0
Share

Get rid of the start, make sure the boulder is tagged as a boulder, and make sure you're using a text and not a 3D text. I just tried it and in the debug window it showed the text "YOU LOSE!" (except I just added it to a game I'm working on so it did it whenever I clicked the left mouse button. I'm not a very good scripter, I just wanted to be of help, try it, let us know.)

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by awest · Jul 16, 2014 at 02:29 AM

I can confirm that both scripts have no errors. I have tested them by themselves with only the necessary objects and they functioned as expected. So it must be something in the way the scene is setup. Or some ghost in the machine.

This is a list of my best guess issues. I know some are redundant. Sorry.

  • Are the scripts in the scene?

  • Is more than one object changing the gui text?

  • Are all of the public variables assigned correctly?

  • Are your tags set with correct spelling/capitolization?

  • Is the On trigger function on an object with a collider set as trigger?

  • Does one object that's colliding have a rigidbody?

  • have you tried restarting Unity or moving everything to a new project?

Others may add to this list if you think of anything.

Comment
Add comment · Show 3 · 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 majormetallica · Jul 16, 2014 at 02:43 AM 0
Share

yes scripts are attatched to the objects in the scene. only one object is assigned to gui text all variables are assigned. tags are set properly. on triger is set to on an empty game object with a collider. the colliding object has a rigidbody. ive tried restarting unity and my computer. but i down know how to move everything into a new project

avatar image awest · Jul 16, 2014 at 03:56 AM 0
Share

To move everything to a new project you can start a new project and copy/paste in assets. I've never actually heard of this solving a problem, but I have heard of many people trying it.

I would get these scripts working and then bring in other assets until it breaks(or doesn't) It might help locate the problem.

One thing just occurred to me. Are you working in 2D? Because then you'd have to use OnTriggerEnter2D().

avatar image majormetallica · Jul 16, 2014 at 05:25 AM 0
Share

no im working in 3d

avatar image
0

Answer by Slider_j · Jul 16, 2014 at 06:03 AM

where you put -

  if(timer <= 0.0f)
 {
       print ("You Lose!");
       timer = timerLimit;
 }

Add in "loseFlag" as a public boolian (which is =false) and you could always swap it for -

 public bool loseFlag = false;
 .
 .
 .
  if(timer <= 0.0f)
 {
      loseFlag = true;
      timer = timerLimit;
  
 }
 



and add in the routine -

 void OnGUI(){
 
    if(loseFlag){
       GUI.Label(Rect(0,0,Screen.width,Screen.height),"You Lose");
    }
 }

hopefully that will work for you! :)

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Javascript debug error stating BCE0044: expecting :, found ';'. Any help? 2 Answers

Where can I start to learn more about coding? 2 Answers

How Do I Fix Unexpected Char 0x201C? (Code Below) 1 Answer

Manipulate gameobjects, pick up & rotate 0 Answers

How to add a peaking system? 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