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 /
  • Help Room /
avatar image
0
Question by nudi93 · Mar 22, 2016 at 09:26 PM · destroyif-statementscounterroll a ball

roll-a-ball Counter issue?

Hello,

I tried to modify roll-a-ball tutorial a little, I have 2 pick ups objects tagged "green" , "red", and "skull" tag for errors and a "flag" taged object for the player to end the game and check for score. the code is attached to my player object .

I have 3 major issues:

  • the counter sometimes shows wrong values? in runtime when collecting an object it decrement the value instead of incrementing"sometimes"?

  • the flag conditions are ignored and the win panel shows whenever the player collides with the flag object, regardless of the if statment? I'm not sure how the if statements block is entered .

  • the stop_player function is ignored, as the player is still able to more :/

my code is the following:

  using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour
 {
     
     public float PlayerSpeed = 4f;
     //text to show counters in UI 
     public Text countText_green;
     public Text countText_red;
     //the score text 
     public Text winText;
     //the fixed required random numbers in UI 
     public Text x_red;
     public Text y_green;
     public GameObject Win_label;
     public Text errorText;
 
     private Rigidbody rb;
     private int count_red;
     private int count_green;
     private int count_error;
     private int reds;
     private int greens;
 
     void Start ()
     {
 
         reds = Random.Range (1, 5);// creates a number between 1 and 12
         greens = Random.Range (1, 5);// creates a number between 1 and 12
 
         rb = GetComponent<Rigidbody> ();
         count_green = 0;
         count_error = 0;
         SetCountText ();
         winText.text = "";
         errorText.text = "";
         x_red.text = "" + reds;
         y_green.text = "" + greens;
     }
     
     void FixedUpdate ()
     {
 
         /*float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
         rb.AddForce (movement * speed );*/
         if (Input.GetKey (KeyCode.UpArrow)) {
 
             //rb.AddForce(Vector3.forward * PlayerSpeed * Time.deltaTime);
             rb.transform.Translate (Vector3.forward * PlayerSpeed * Time.deltaTime); 
         }
         if (Input.GetKey (KeyCode.DownArrow)) {
             rb.transform.Translate (
                 Vector3.back * PlayerSpeed * Time.deltaTime); 
         }
         if (Input.GetKey (KeyCode.RightArrow)) {
             rb.transform.Rotate (
                 new Vector3 (0, 100f, 0) * Time.deltaTime); 
         }
         
         if (Input.GetKey (KeyCode.LeftArrow)) {
             rb.transform.Rotate (
                 new Vector3 (0, -100f, 0) * Time.deltaTime); 
         }
     
     }
     
     void OnTriggerEnter (Collider other)
     {
         if (other.gameObject.CompareTag ("Green")) {
             other.gameObject.SetActive (false);
             count_green = count_green + 1;
             countText_green.text = count_green.ToString ();
         }
         
         if (other.gameObject.CompareTag ("Red")) {
             other.gameObject.SetActive (false);
             count_red = count_red + 1;
             countText_red.text = count_red.ToString ();
         }
         
         if (other.gameObject.CompareTag ("skull")) {
             other.gameObject.SetActive (false);
             count_error = count_error + 1;
         }
         
         
         if (other.gameObject.CompareTag ("flag")) {
 
             //if player still didn't reach required numbers
             if (count_red < reds || count_green < greens) {
                 //continue playing
             }
 
         //wining cases 
             else if (count_red == reds 
                 && count_green == greens) {
                     
                 Stop_player ();
 
                 Win_label.gameObject.SetActive (true);
                 winText.gameObject.SetActive (true);
                 errorText.text = "" + count_error.ToString (); 
 
                 //total score is 10 for each error decrement 1 
                 int score = 10 - (count_error); 
                 winText.text = score.ToString ();
 
             }
             //if player collected more than required 
 
             else if (count_red >= reds || count_green >= greens) { 
             
                 Stop_player ();
 
                 int extra_red = 0; 
                 int extra_green = 0; 
 
                 if (count_red > reds) 
                     extra_red = count_red - reds; 
                 if (count_green > greens) 
                     extra_green = count_green - greens;
 
                 //add wrong numbers to count errors to decrement score
                 count_error = count_error + extra_red + extra_green; 
 
                 Win_label.gameObject.SetActive (true);
                 winText.gameObject.SetActive (true);
                 errorText.text = "" + count_error.ToString (); 
 
                 //total score is 10 for each error decrement 1     
                 double score_D = 20 - count_error; 
                 string score_S = score_D.ToString ();
                 int score = int.Parse (score_S);
                 winText.text = score.ToString (); 
             }
             //}//end wining case
         }
 
         
     }
 
     void Stop_player ()
     {
         Destroy (rb);
         PlayerSpeed = 0f;
     }
     
     void SetCountText ()
     {
         countText_green.text = count_green.ToString ();
         countText_red.text = count_red.ToString ();
         
     }
 }


Any help would be grateful. thank you <3

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

51 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

hey im wondering how to destroy an object after a certain score... its a 'wall' but i want it be destroyed after say 100 but it seems to be destroyed every time i touch it with the player any help at all would be appreciated.... heres what i have 0 Answers

Am I misunderstanding how this code should function? 1 Answer

mathf.abs(0) is higher than 90, why? 1 Answer

Can't Locate gameobject in array 3 Answers

problem with lists 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