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 kayonkykt · Dec 04, 2016 at 10:46 AM · androidmobilescore system

How to solve "Target left counting"

The following code is about counting target left , it can count on first time but the rest is not(3 targets) maybe something went wrong *I'm a beginner for this

void OnTriggerEnter(Collider other) {

     if (other.gameObject.tag == "Ball")
     {
         GameObject effect = Instantiate(prefabEffect) as GameObject;
         effect.transform.position = this.transform.position;
         Destroy(gameObject);
         Handheld.Vibrate();
         
         targetTotal -= 1;
         targetText.text = "Target left: " + targetTotal;


         if (targetTotal == 0)
             winText.enabled = true;

     }

    
    
 }
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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Paricus · Dec 04, 2016 at 08:32 PM

you're destroying the script that holds the target total variable, you should create a new script that doesn't get destroyed and use that to hold if (targetTotal == 0)

  public CounterScript counterScript;

  if (other.gameObject.tag == "Ball")
  {
      GameObject effect = Instantiate(prefabEffect) as GameObject;
      effect.transform.position = this.transform.position;
      counterScript.TargetTotal();
      Handheld.Vibrate();
      Destroy(gameObject);
     }
  }

Counter Script

 public class CounterScript:MonoBehavior
 {
       int targetTotal = <value>

      public void TargetTotal()
         {
               targetTotal -= 1;
               targetText.text = "Target Left: " + targetTotal

              if(targetTotal == 0)
                   winText.enable = true;
         }
 }


Add an empty game object to the scene and attach the counter script, then drag that game object onto the game object that holds your script, in the inspector window.

Hope this helped

Comment
Add comment · Show 11 · 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 kayonkykt · Dec 05, 2016 at 11:38 AM 0
Share

I've already try to make your instruction, but it got an error is occurred, maybe I did something wrong, would you $$anonymous$$d to tell me more in deep about detail. Sorry for my beginner skill guys

avatar image Paricus kayonkykt · Dec 05, 2016 at 11:54 AM 0
Share

could you submit you error? I don't write the scripts in a compiler, so it may just be syntax errors

avatar image kayonkykt Paricus · Dec 05, 2016 at 12:39 PM 0
Share

alt text

capture.jpg (27.0 kB)
Show more comments
Show more comments
avatar image Paricus kayonkykt · Dec 05, 2016 at 01:57 PM 0
Share

you need to establish the wincanvas variable

 public GameObject wincanvas;

you should't give up, its all part of learning to program

avatar image kayonkykt Paricus · Dec 05, 2016 at 02:20 PM 0
Share

How could I send my full script cause of some syntax may error. I have one script of this problem on my target game object

Show more comments
Show more comments
avatar image Paricus kayonkykt · Dec 06, 2016 at 10:07 AM 0
Share

Ok, I'm not completely sure what you already have in the project, but i'll write scripts as if you're just starting the project.

Count Script Create an empty game object and attach this script

    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    
    public class ScoreCounter : $$anonymous$$onoBehaviour {
    public GameObject winText;
    int enemyCount = 3;
    
        public void checkEnemiesRemaining()
        {
        enemyCount --;
        if(enemyCount == 0)
           winText.SetActive(true);
        }


Target Script

  using UnityEngine;
  using UnityEngine.UI;
  using System.Collections;
 
 public class PlayerController : $$anonymous$$onoBehaviour {
 
 public ScoreCounter scoreScript; //make sure to drag empty game object (hierarchy view) with the ScoreCounter script into here (inspector view)
 
 void OnCollisionEnter(Collision other)
 {
      if (other.gameObject.tag == "Ball")
      {
          GameObject effect = Instantiate(prefabEffect, transform.position, prefabEffect.transform.rotation) as GameObject;
          scoreScript.SetCountText()
          Handheld.Vibrate();
          Destroy(gameObject);
          
      }
  }
 }
 


avatar image
0

Answer by JeffHardddyyy · Dec 04, 2016 at 04:48 PM

I presume you are doing the Roll-A-Ball Course,

So props on that. try this :

 /*Please know that I just copied and pasted it from when I did the Roll-A-Ball course, I did explain it. Take the things that you need from it, I did explain it for myself and I do have a disability so if it doesn't make sense, please reply, I will go more in depth. I did the entire script so if you are like "What is this for" you can copy that selection, then if you go "Oh, I may need this too!" I allowed that as well. Good luck! If you reply with a comment, I will try my best to answer, Good luck in Unity dude!*/
 
 
 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     public float speed;
     public Text countText;
     //The text that will be counting
     public Text winText;
     //The text that will display when you get 'x' points
 
     private Rigidbody rb;
     private int count;
 
     void Start ()
     {
         rb = GetComponent<Rigidbody>();
         count = 0;
         SetCountText ();
         winText.text = "";
     }
     //Sets the count text to 0. Change "Count" to however many you want them to start with.
 
     void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
 
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
 
         rb.AddForce (movement * speed);
     }
     // This part of the script includes the movement.
 
     void OnTriggerEnter(Collider other) 
     {
         if (other.gameObject.CompareTag ( "Pick Up"))
         {
             other.gameObject.SetActive (false);
             count = count + 1;
             SetCountText ();
         }
     }
     //If anything touches anything with the tag "Pick Up" it will +1 the count.
 
     void SetCountText ()
     {
         countText.text = "Count: " + count.ToString ();
         if (count >= 12)
         {
             winText.text = "You Win!";
         }
     }
     // Says the count, and if the count is greater than / equal to, It will say "You Win!"
     // Change the "12" to assign the points until you want them to win.
 }
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 kayonkykt · Dec 05, 2016 at 11:02 AM 0
Share

I use your SetCountText() method and it gets the same result

avatar image
0

Answer by tareque · Dec 05, 2016 at 02:11 AM

Try this:

void Update () { // The code you posted }

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 kayonkykt · Dec 05, 2016 at 11:40 AM 0
Share

it doesn't work. Thanks

avatar image
0

Answer by kayonkykt · Dec 06, 2016 at 12:59 PM

Error again, this is my target script

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class goal : MonoBehaviour {

 public GameObject prefabEffect;
 public AudioClip shoot;
 private AudioSource audioPlayer;


 public Text targetText;
 public Canvas wincanvas;
 int enemyCount = 3 ;
 public ScoreCounter scoreScript;


 // Use this for initialization
 void Start ()
 {

     targetText.text = "Target left: " + enemyCount;
     audioPlayer = this.GetComponent<AudioSource>();

     wincanvas.enabled = false;

 }


  void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Ball")
     {
         GameObject effect = Instantiate(prefabEffect) as GameObject;
         effect.transform.position = this.transform.position;

         audioPlayer.PlayOneShot (shoot);

         scoreScript.checkEnemiesRemaining ();

         Destroy(gameObject);

         Handheld.Vibrate();


     }
           
 }


 // Update is called once per frame
 void Update ()
 {
     this.transform.localRotation =
         Quaternion.Euler(0, 1, 0) * this.transform.localRotation;

 }

}

and it's your target script

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class ScoreCounter : MonoBehaviour { public Canvas wincanvas; public Text targetText; int enemyCount = 3 ;

 public void checkEnemiesRemaining()
 {
     enemyCount--;
     targetText.text = "Target left: " + enemyCount;
     if(enemyCount == 0)
         wincanvas.enabled = true;
 }

@Paricus

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 Paricus · Dec 06, 2016 at 01:05 PM 0
Share

whats the error

avatar image kayonkykt Paricus · Dec 06, 2016 at 01:21 PM 0
Share

cant attach any script to game object

avatar image Paricus kayonkykt · Dec 06, 2016 at 01:21 PM 0
Share

are you not getting an error in the console view

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

6 People are following this question.

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

Related Questions

Mobile bloom kills performance 1 Answer

Black Material when camera rotate 1 Answer

Shadows problem on mobile 1 Answer

MouseUp, MouseEnter and MouseExit work on android? 2 Answers

how to make unlock/lock sytem for player selection? 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