Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Sadakos Boyfriend · Sep 21, 2011 at 06:38 PM · getcomponentontriggerenterguitext

Updating GUIText

I'm trying to make a very simple Lap counter for a racing game.

As i'm posting a question here, you can prolly guess that i have a problem (you smart cookies, you!).

I have a GUIText called "Lap Text" which displays the players laps (this part is fine), it has the folowing code:

var Counter : int = 0;

function Update (){

 Counter=0;
 guiText.text = "Lap: "+Counter;
 

} The part i'm having trouble with is updating the 'Counter' variable when a trigger is passed through by the player. I'm currently trying a GetComponent JS script:

function OnTriggerEnter (myTrigger : Collider) {

if(myTrigger.gameObject.name == "Start"){

gameObject.Find("Lap Text").GetComponent(MonoBehaviour); MonoBehaviour.Counter +1;

  }

} What's supposed to happen is that the player passes through the trigger (called 'Start' and the GUIText 'Counter' is increased by 1, but it isn't working.

Please tell me where i'm going wrong :(

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 aldonaletto · Sep 21, 2011 at 06:57 PM

You should use the script name instead of MonoBehaviour - unless the first script is called MonoBehaviour.js and is the only one with this name in your project - and assign the result of GetComponent to a variable of the same type (the script type is its name without quotes or extension), then use it as the script reference. Supposing the first script is called LapDisplay.js:

function OnTriggerEnter (myTrigger : Collider) {

 if(myTrigger.gameObject.name == "Start"){
     var scrpt: LapDisplay = gameObject.Find("Lap Text").GetComponent(LapDisplay);
     scrpt.Counter += 1; // that's the way to increment the Counter variable 
 }

} NOTE: You may also notice some hickups when any car passes the Start - searching for some object among a thousand others may be too slow. The fastest way in this case would be to have a GUIText public variable and assign it at Start or at the Inspector instead of look for it every time a vehicle crosses the line:

var lapText: GUIText;

function Start(){ lapText = GameObject.Find("Lap Text"); }

function OnTriggerEnter (myTrigger : Collider) {

 if(myTrigger.gameObject.name == "Start"){
     var scrpt: LapDisplay = lapText.GetComponent(LapDisplay);
     scrpt.Counter += 1; // that's the way to increment the Counter variable 
 }

}

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 Sadakos Boyfriend · Sep 23, 2011 at 02:12 PM 0
Share

Thanks for help AND the additional information (the 'function Start' bit.

Helped alot :)

avatar image
0

Answer by FLASHDENMARK · Sep 21, 2011 at 07:04 PM

One similar but simple solution is to mark the variable 'counter' as static(which means that it can be accessed from all other scripts) and add to that variable. The only thing you need in order to access the variable is(to mark it as static of course) know the name of the script and the name of the variable you are accessing. So: nameOfScript.theNameOfTheVariable += 1;

 //Call this script Counter
 
 static var counter : int = 0;
 
 function Update (){    
     guiText.text = "Lap: "+Counter;
     
 }
 //And you can call this script what you want
 function OnTriggerEnter (myTrigger : Collider)  {
 
 if(myTrigger.gameObject.name == "Start"){
 //Access the variable by the script name and the variable name
 Counter.counter += 1;
      
      }
 }

NOTE: In your script you are calling "Counter = 0;" That will always result in the variable to be 0 and never change. You should call that in awake so when the game starts the variable will be 0.

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 FLASHDENMARK · Sep 21, 2011 at 07:04 PM

One similar but simple solution is to mark the variable 'counter' as static(which means that it can be accessed from all other scripts) and add to that variable. The only thing you need in order to access the variable is(to mark it as static of course) know the name of the script and the name of the variable you are accessing. So: nameOfScript.theNameOfTheVariable += 1;

 //Call this script Counter
 
 static var counter : int = 0;
 
 function Update (){    
     guiText.text = "Lap: "+Counter;
     
 }
 //And you can call this script what you want
 function OnTriggerEnter (myTrigger : Collider)  {
 
 if(myTrigger.gameObject.name == "Start"){
 //Access the variable by the script name and the variable name
 Counter.counter += 1;
      
      }
 }

NOTE: In your script you are calling "Counter = 0;" That will always result in the variable to be 0 and never change. You should call that in awake so when the game starts the variable will be 0.

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 Sadakos Boyfriend · Sep 23, 2011 at 02:14 PM 0
Share

Thanks for your help, got it sorted now!

Also thanks for letting me know about my 'counter = 0' gaff! :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

GUIText spawning when gameobject is destroyed. 1 Answer

accessing script components from external objects 2 Answers

How can I increase Health Value on trigger enter, to another game object? 3 Answers

Damager Powerup issue - NullReferenceException: Object reference not set to an instance of an object 2 Answers

Setting thrown object to kinematic after hitting target? 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