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
-1
Question by Steve Cogbill · Feb 13, 2014 at 02:00 AM · errortriggercountcollect

Collecting 2 items at a time by mistake.

I have a very simple game where the goal is to collect 15 stars. Everything works fine except that some of the stars count for 2 points instead of 1. I tried deleting the stars that get counted twice, but it changes every time. If I play it, a star will be counted once, then the next time I play it, maybe that star gets counted as 2 points. Its very random and I dont know why its happening. I only want each star to be worth 1 point.

 using UnityEngine;
 using System.Collections;
 
 public class Collect : MonoBehaviour 
 {
     public GUIText countText;
     public GUIText winText;
     private int count;
     
     void Start ()
     {
         count = 0;
         SetCountText ();
         winText.text = "";
     }
     
     void OnTriggerEnter(Collider other)
     {
         if(other.gameObject.tag == "Star")
         {
             other.gameObject.SetActive(false);
             count = count + 1;
             SetCountText ();
         }
     }
     
     void SetCountText ()
     {
     countText.text = "Stars: " + count.ToString();
         if(count >= 15)
         {
             winText.text = "YOU WIN!";
         }
     }    
 }
 
Comment
Add comment · Show 6
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 getyour411 · Feb 13, 2014 at 02:00 AM 1
Share

Show relevant code

avatar image Hoeloe · Feb 13, 2014 at 08:37 PM 0
Share

You are given the option to edit your questions, or at the very least, to comment. Do not post updated information as an answer, if only because it makes it look like your question has already been answered, so people are less likely to read it.

avatar image Steve Cogbill · Feb 13, 2014 at 08:54 PM 0
Share

Sorry, I haven't used the answers very much, I just wanted some help. The code is there now. Anyone have any advice?

avatar image MousePods · Feb 13, 2014 at 09:04 PM 0
Share

It has to be something other than your code...At least imo. I don't see anything that would be causing it with the code you gave :|

avatar image Steve Cogbill · Feb 13, 2014 at 09:51 PM 0
Share

Thats what I was thinking it might be, but I couldnt figure out what else could be causing it Ive also noticed that some of them count for 2 points or 3 points or even bigger numbers like 23 points. It just seems so random and I cant figure out why they arent couting by 1 each time.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by BladeWarrior · May 12, 2020 at 11:16 AM

A friend helped me fix this by taking out the "count + 1" and have it calucalate by getting the total and subtracting the collect action which gives you how many was collected. Code below is working for me.

 public Text CountText;
     private int total;
     private int nextScene;
 
     // Use this for initialization
     void Start()
     {
         total = GameObject.FindGameObjectsWithTag("collect").Length;
         UpdateCount(total);
         nextScene = SceneManager.GetActiveScene().buildIndex + 1;
     }
     
     void OnTriggerEnter(Collider ThingCollidedWith)
     {
         if (ThingCollidedWith.gameObject.CompareTag("collect"))
         {
             Destroy(ThingCollidedWith.gameObject);
         }
     }
 
     void Update()
     {
         var count = GameObject.FindGameObjectsWithTag("collect").Length;
         UpdateCount(count);
         if (count == 0)
         {
             SceneManager.LoadScene(nextScene);
         }
     }
 
     void UpdateCount(int count)
     {
         CountText.text = "You've collected " + (total - count) + " / " + total;
     }
 
 
 }
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

21 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

Related Questions

Error in..."calling"(?) script 0 Answers

Unity Script Editor Not Working 1 Answer

Errors while assigning material to other object's by scripting. 1 Answer

Error line 22! Help. i dont know why? 1 Answer

Where is this (-1) coming from in my change level script? 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