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 SimonDenton · Dec 16, 2011 at 09:53 AM · racecheckpoints

Check my race code please

There seem to be problems with accessing the PlayerObject data as well as displaying the number of current checkpoints and laps.

Code for the game object 'RaceCheckpoint', touch this to make a checkpoint progression

 public int PrevCheckNo =0; //So laps have to be obtained in order e.g the previous checkpoint has to be 1 in order to do + 1 and make this prev checkpoint a 2
     
     void Awake ()
     {
         
     }
     void OnCollisionEnter() {
     
         if (currentCheckpoint = PrevCheckNo)
         {
         GameObject PlayerObject = GameObject.FindWithTag("Player");
         PlayerObject.currentCheckpoint = (currentCheckpoint + 1);
         }
     }

Code for class 'PlayerObject', which is the player with all components

 private static  int currentCheckpoint = 0; //Current checkpoint
 private static  int currentLap = 0; //Current lap
 
 public int LapTotal = 0; //How many laps in the race overall?
 public int CheckpointTotal =0; // How many checkpoints in this race?
 
 public string CurrentCheckText = "Current Checkpoint";
 public string CurrentLapText = "Current Lap";
 
 void Update ()
 {
 
 // Race Checkpoint System
 if (currentCheckpoint > CheckpointTotal)
         {
             currentLap = (currentLap + 1); //Add a lap
             currentCheckpoint =0; //Reset checkpoint total
         }
 if (currentLap > LapTotal)
         {
             //End of our turn in this race
             
         }
         
         }
 void OnGUI() {
         CurrentCheckText = GUI.TextField(new Rect(10, 10, 200, 20), CurrentCheckText: currentCheckpoint, 25);
         
         
         CurrentLapText = GUI.TextField(new Rect(10, 50, 200, 20), CurrentLapText: currentLap, 25);
     }
     }
Comment
Add comment · Show 2
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 BiG · Dec 16, 2011 at 10:22 AM 0
Share

You need GetComponent() to access the variable. I've never tried it with a class, so I suppose it will be a little more tricky. by the way, try GameObject.FindWithTag("Player").GetComponent(PlayerObject).currentCheckpoint = currentCheckpoint + 1; ins$$anonymous$$d that PlayerObject.currentCheckpoint = (currentCheckpoint + 1); in the first script. I hope that my syntax is correct, but it should point you in the right direction.

avatar image SimonDenton · Dec 16, 2011 at 10:29 AM 0
Share

Thanks for the contribution BiG.

Unfortunately both your good attempt line and the line "if (currentCheckpoint = PrevCheckNo)" have contextual errors (variables not existing in the context)in addition with 'The best overloaded method match for `UnityEngine.GameObject.GetComponent(System.Type)' has some invalid arguments'.

At least the PlayerObject class has no errors, with the void GUI part as a comment as I am leaving that for later.

We're in some sort of trap. But I appreciate your guys helping me get out of it :D

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SimonDenton · Jan 03, 2012 at 04:35 AM

I managed to solve this issue with the help of a friend. Here is the working player code:

 void OnTriggerEnter(Collider collision)
 {
         //Update checkpoint number
 
     
         RaceCheckPointScript Checkpoint;
         print(collision.gameObject.tag);
         Checkpoint = collision.gameObject.GetComponent<RaceCheckPointScript>();
         if (Checkpoint)
             if (currentCheckpoint == Checkpoint.prevCheckNo)
             {
                     Debug.Log("Checking if checkpoint is prev");
                     currentCheckpoint++;
             }
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
1

Answer by ks13 · Dec 16, 2011 at 09:58 AM

Hi, try removing "static" between "private" and "int", for currentLap and currentCheckpoint.

Comment
Add comment · Show 5 · 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 SimonDenton · Dec 16, 2011 at 10:08 AM 0
Share

Thank you for your input ks13. I still have errors though as listed below:

Assets/Scripts/RaceCheckPoint.cs: error CS1061: Type UnityEngine.GameObject' does not contain a definition for currentCheckpoint' and no extension method currentCheckpoint' of type UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)

Assets/Scripts/RaceCheckPoint.cs: error CS0103: The name `currentCheckpoint' does not exist in the current context

avatar image ks13 · Dec 17, 2011 at 03:32 PM 0
Share

That's because you made it private...put it public so other classes will be able to acess them.

avatar image SimonDenton · Dec 20, 2011 at 04:26 AM 0
Share

Thanks again but still the contextual errors.

avatar image ks13 · Dec 22, 2011 at 08:16 AM 0
Share
 if (currentCheckpoint = PrevCheckNo)
        {
          GameObject PlayerObject = GameObject.FindWithTag("Player");
          PlayerObject.currentCheckpoint = (currentCheckpoint + 1);
        }

Shouldn't this be

     GameObject PlayerObject = GameObject.FindWithTag("Player");
     if (PlayerObject.currentCheckpoint = PrevCheckNo)
        {
           PlayerObject.currentCheckpoint = (PlayerObject.currentCheckpoint + 1);
        }

I mean, where do you create/get your currentCheckpoint variable? the only declaration i see is in PlayerObject, not in RaceCheckPoint.

avatar image SimonDenton · Dec 22, 2011 at 09:20 AM 0
Share

Yes I declare currentCheckpoint in the PlayerObject script and the RaceCheckPoint is meant to change that property during the OnTriggerEnter(changed from on OnCollisionEnter).

Thanks for your logical lines! Apart from UnityEngine.GameObject' does not contain a definition for currentCheckpoint' and no extension method currentCheckpoint' of type UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?) it is good to go!

avatar image
1

Answer by ks13 · Dec 22, 2011 at 02:39 PM

Ok, you're actually just confusing me more, and your code of RaceCheckPoint is weird :
If you read this you'll see that OnCollisionEnter actually takes a parameter, which is passed by unity and not your scripts. that way you can get the transform of the collider that triggered the event. So you can use a code like :

void OnCollisionEnter(Collision collision) {
   scriptName = col.transform.GetComponent("scriptName");
   if (scriptName.currentCheckpoint == PrevCheckNo) //Variables names usually start with lower case characters, and Methods/Functions with upper, that way it's easier to distinguish them
      {
        scriptName.currentCheckpoint += 1;
      }
   else
   DisplaySomeErrorMessage();
}

Don't forget to use the script's name for the search, because when i read carefully i think your problem is you're trying to use a Transform as a script, which is wrong and why it dsplays those errors.

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 SimonDenton · Dec 23, 2011 at 12:52 AM 0
Share

Sorry about the confusion. Checkpoint is the name of an object that is a collider trigger. RaceCheckPointScript is the script attached to it. The checkpoint has only one variable; prevCheckNo. The player script has a variable currentCheckpoint. If the integer value of currentCheckpoint is equal to the integer value of variable prevCheckNo, then currentCheckpoint gets +1. So:

 //Update checkpoint number
 
         void OnCollisionEnter(Collision collision) {
     if(collision.gameObject.tag == "Checkpoint"){
         RaceCheckPointScript RaceCheckpoint;
         RaceCheckpoint = gameObject.GetComponent("RaceCheckPointScript") as RaceCheckPointScript;
 if (currentCheckpoint == RaceCheckpoint.prevCheckNo)
         {
             currentCheckpoint = currentCheckpoint +1;
             
         }
     }
     }
avatar image ks13 · Dec 23, 2011 at 08:11 AM 0
Share

That seems correct but, that is a code applicable on PlayerObject, not CheckPoint. I thought the code was to be put on CheckPoint, but that's up to you to decide.

avatar image SimonDenton · Dec 23, 2011 at 08:14 AM 0
Share

Yeah I decided for PlayerObject to do all the work and let the checkpoint just have its variable integer and nothing more. And you know what? I got NO ERRORS! Problem? currentCheckpoint doesn't change. However, the laps and so forth work beautifully!

avatar image ks13 · Dec 23, 2011 at 09:34 AM 0
Share

Have you checked the begining value of currentCheckpoint and the value of the first PrevCheckNo?

avatar image SimonDenton · Dec 23, 2011 at 10:11 AM 0
Share

Yes in the player code currentCheckpoint is 0 and on the first Checkpoint object the PrevCheckNo is 0 as well. So when we collide with this one, since they are both equal, the +1 operation should work so that currentCheckpoint is 1. Then when we touch the next checkpoint object (PrevCheckNo = 1) it adds again etc.

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

Setting up player positions in a racing game 1 Answer

How Do I Set The Number Of Laps? 1 Answer

How do I make laps in a racing game? 5 Answers

Motorcycle Controller for Mobile 0 Answers

How would I efficiently implement race positions? 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