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 justify · Sep 08, 2011 at 06:39 AM · triggerongui

Adding a value after a trigger on

I am making a race car and I just want to add a value on my variable "Lap" once the car triggered a gameobject which act as my finish line

Simply i just want to do this

var lap = 0;

function OnTriggerEnter(other : Collider) { if(other.gameObject.tag == "Player") { lap +1; } }

then afterward i want to use the said value (lap) to show on my GUI and if the value is equals to 3 show a "Complete Race" text on my GUI

I used

var lapCounter : GameObject;

function OnGUI () { var curLap = lapCounter.GetComponent("lapCounter").lap; GUI.Label (Rect (Screen.width-100,50,120,20), "Lap:" +curLap);

and

var playerlap = 1; private var guiEnable = false;

function OnGUI() { if (guiEnable) { GUI.Label (Rect (Screen.width/2,Screen.heigth,400,200),"RACE COMPLETE"); } } function Update () { if (playerlap == 3) { guiEnable = true; } }

I don't know what seems to be the problem...

Comment
Add comment · Show 4
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 Adam-Buckner ♦♦ · Sep 08, 2011 at 08:47 PM 0
Share

First off, has anyone noticed that posting code here just sux when it comes to formatting?

avatar image mcarriere · Sep 08, 2011 at 08:57 PM 0
Share

You're not really going into detail about the problem you're having @justify. Is nothing showing up for you. Can you also be a bit more specific which component is which, and possibly clean up your code pastes a little bit?

avatar image Adam-Buckner ♦♦ · Sep 08, 2011 at 09:20 PM 0
Share

$$anonymous$$y comment was not against justify, but supporting justify and the difficulty posting code on Answers. Pasting code here is a crap shoot whether it will format at all. One gets a funny "paste code here" marker and more than 75% of the time it just looks like spilt garbage when it's posted.

avatar image Adam-Buckner ♦♦ · Sep 09, 2011 at 01:38 PM 0
Share

$$anonymous$$ostly for justify: http://answers.unity3d.com/questions/165232/best-practices-for-posting-code.html

To fix your code, it seems the best practice is to fix it in a text editor, paste it, and then format it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Adam-Buckner · Sep 08, 2011 at 08:58 PM

I would approach this by having the lap counter variable in the main script, and have the lap counter trigger set it.

On your trigger you could have:

 var myGameManager : GameManager;
 function OnTriggerEnter(other : Collider) {
     if (other.gameObject.tag == "Player") {
         myGameManager.lap +1;
     }
 }

(Hate this lack of code formatting...pasted it:) http://pastebin.com/DgMYeYgV

Then I would populate that slot in the inspector by grabbing your GameManager Object with the GameManager script on it and dragging it into the slot on the trigger.

This way the GameManager script do all of the manipulation and heavy lifting with a local value. With:

 var lap : int; 

... in your main GameManager, having Update () and OnGUI() doing the work should be seamless.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

enable a canvas on trigger 2 Answers

Trigger 'if' statement while in TextField 2 Answers

How to have OnGUI element in a OnTriggerEnter function? 3 Answers

OnGUI not working? 1 Answer

Can't click gameobject when over another trigger? 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