• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Enable ChildObject of Player with OnTriggerEnter? Or a whole different method? 0 Answers

Is there a way to find other's components using OnTriggerEnter? 1 Answer

Problems with GetComponents 1 Answer

Can I access a script OnTrigger WITHOUT using getcomponent? 1 Answer

Trouble with communication between scripts 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges