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 Whipinsnapper · May 04, 2016 at 03:06 PM · gameobjectcollision detection

OnCollisionEnter Won't Edit Other Variables

Alright, so I have created a little racing game. I was making a checkpoint, so that you couldn't cross the finish line and get a lap without passing the checkpoint. At first I just wanted it to be an OnTriggerEnter, which worked fine. But in 2 player, that wouldn't work. I have 3 scripts (1 for the finish line, 1 for the checkpoint for P1, and 1 for the checkpoint for P2), and when I pass the checkpoint, it won't edit the variables! Here's the script for the checkpoint for P1:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
  
 public class Checkpoint : MonoBehaviour {
  
  
     public Text Checker;
 public Text Reseter;
     private int checkpoint;
 private int checkreset;
  
     // Use this for initialization
     void Start () {

         Checker.text = "0";
         checkpoint = 0;
 
  
     }
  
  
     // Update is called once per frame
     void Update () {

     if(Reseter.text == "1"){
         checkpoint = 0;
     }
         

     if(checkpoint == 1){
         Checker.text = "1";
     }


     }
  
  
     void OnCollisionEnter (Collision col) {
         if(col.gameObject.name == "Player 1"){
         checkpoint = 1;
             Reseter.text = "0";
         }
  
     }

  
      
  
 }
  

And for P2:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
  
 public class Checkpoint2 : MonoBehaviour {
  
  
     public Text Checker2;
 public Text Reseter2;
     private int checkpoint;
 private int checkreset;
  
     // Use this for initialization
     void Start () {

         Checker2.text = "0";
         checkpoint = 0;
 
  
     }
  
  
     // Update is called once per frame
     void Update () {

     if(Reseter2.text == "1"){
         checkpoint = 0;
     }
         

     if(checkpoint == 1){
         Checker2.text = "1";
     }

     }
  
  
     void OnCollisionEnter (Collision col) {
  
         if(col.gameObject.name == "Player 2"){
     checkpoint = 1;
         Reseter2.text = "0";

     }
  
     }

  
      
  
 }

Here's the script for the Finish Line:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
  
 public class Lap : MonoBehaviour {
  
  
     public Text MyText;
 public Text MyText2;
 public Text Checker;
 public Text Checker2;
 public Text Reseter;
 public Text Reseter2;
     private int score;
 private int score2;
 private int counter;
 private int counter2;
  
     // Use this for initialization
     void Start () {
     
         score = 0;
     score2 = 0;
     counter = 1;
     counter2 = 1;
     Reseter.text = "0";
     Reseter2.text = "0";
 
  
     }
  
  
     // Update is called once per frame
     void Update () {

         MyText.text = "Lap: "+ counter;
     MyText2.text = "Lap: "+ counter2;
         
         if(counter >= 3){
             MyText.text = "You beat the race!";            
             if(Input.GetMouseButtonDown(0)){
                 Application.LoadLevel("Menu");

             }

         }

     if(counter2 >= 3){
         MyText2.text = "You beat the race!";
     if(Input.GetMouseButtonDown(0)){
         Application.LoadLevel("Menu");
             }
        }
  
     }
  
  
     void OnCollisionEnter (Collision col) {
          if(col.gameObject.name == "P1"){
     if(Checker.text == "1"){
             score = score + 1;
             counter = score + 1;
         Checker.text = "0";
         Reseter.text = "1";
     }
     }

     if(col.gameObject.name == "P2"){
         if(Checker2.text == "1"){
         score2 = score2 + 1;
         counter2 = score2 + 1;
         Checker2.text = "0";
         Reseter2.text = "1";
     }
     }
  
     }


  
      
  
 }
  

Is there a mistake I made that doesn't show up in the editor? Thanks in advance.

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Trying to get collision detection between a game object and signed distance field [SDF] 0 Answers

if statement not working when detecting collision between two prefabs 1 Answer

Collision 2D doesnt work 0 Answers

how do i create scripts for creating question and answer system? 0 Answers

Why my capsule collider sometimes stucks in other colliders? 0 Answers


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