• 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 kitallen23 · Aug 12, 2014 at 05:12 AM · colliderprefabtrigger

Multiple triggers updating one GUIText

I'm working on a jetpack-type side-scrolling game to learn the basics of Unity and scripting. I'm currently stuck trying to get the 'score' to work.

I have two types of coins that I want the player to be able to collect as they play. One more common coin named 'coin', and another, less-common coin named 'megaCoin'. I can successfully get the score to count up by one each time 'coin' is triggered, but cannot manage to get the score to go up by a further 10 when 'megaCoin' is triggered. Each GameObject (prefab) has its own tag; Coin and MegaCoin.

My code for the counting of the 'coin' is as follows:

     public GUIText coinCount;
     private float coins;
 
     public void Update()
     {
         coinCount.text = coins.ToString("f0"); 
     }
 
     public void OnTriggerEnter(Collider coll)
     {
         if (coll.gameObject.tag == "Coin")
         {
             print("Coin collected!");
             Destroy(coll.gameObject);
             coins = coins + 1f;
         }
     }

Now with my 'megaCoin', I'm trying to link the SAME GUIText variable as in the 'coin' code, so I can get both scripts to contribute to the same number over time. This is my 'megaCoin' script:

     public GUIText coinCount;
     private float coins;
 
     public void Update()
     {
         coinCount.text = coins.ToString("f0");
     }
 
     public void OnTriggerEnter(Collider megaColl)
     {
         if (megaColl.gameObject.tag == "MegaCoin")
         {
             print("MEGACOIN Collected!");
             Destroy(megaColl.gameObject);
             coins = coins + 10f;
         }
     }

The problem is, as stated above, the 'coin' script works with increasing the number in the GUIText, however the 'megaCoin' script DOES NOT increase the number - triggering a 'megaCoin' does everything except increase the count (successfully prints, destroys etc.).

Where have I gone wrong? Thanks!!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by watswat5 · Aug 12, 2014 at 06:09 AM

You don't need two separate scripts for this. In fact, one script is much easier!

One solution is to have the player hold one coin script. When the player collides with a coin, the OnCollisionEnter event will fire. Check the tag of the object (coin or megaCoin) to decide the value of the coin.

I've coded a simple script to accomplish this:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerCoins : MonoBehavior {
 
     public float coinScore;
     public GUIText scoreKeeper;
 
     void Update()
     {
         scoreKeeper.text = "" + coinScore;
     } 
 
     void OnCollisionEnter(Collision c)
     {
         if(c.transform.tag == "Coin")
         {
             coinScore++;
             Destroy(c.transform.gameObject);
         }
         else if(c.transform.tag == "MegaCoin")
         {
             coinScore += 10;
             Destroy(c.transform.gameObject);
         }
     }
 }
 

Remove the other two scripts and apply this script to the player.

If you're using triggers, just use OnTriggerEnter(Collider c) instead of OnCollisionEnter(Collision c).

Keeping track of one score variable is MUCH easier than having two separate ones, and the overall complexity of the scripting is also lower.

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
0

Answer by zharik86 · Aug 12, 2014 at 06:03 AM

First, you dont't initialization your variable "coins".Second, create one script base on your two. Example see below:

  public GUIText coinCount;
  private float coins;

  void Start() {
   coins =0;
  }

  void Update() {
   coinCount.text = coins.ToString("f0"); 
  }
 
  void OnTriggerEnter(Collider coll) {
   if (coll.gameObject.tag == "Coin") {
    print("Coin collected!");
    Destroy(coll.gameObject);
    coins = coins + 1f;
   } else if (coll.gameObject.tag == "MegaCoin") {
    print("MEGACOIN Collected!");
    Destroy(coll.gameObject);
    coins = coins + 10f;
   }
  }

And don't forget attach this script to any object your scene, for example, MainCamera. I hope that it will help you.

Comment
Add comment · Show 2 · 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 watswat5 · Aug 12, 2014 at 07:09 AM 0
Share

The script needs to be added specifically to the gameObject that will be colliding with the coins (I.$$anonymous$$ the player).

avatar image zharik86 · Aug 12, 2014 at 07:02 PM 0
Share

@watswat5 Of course you are right. The script shall be connected to object with a collider. But who knows, the component can the collider is available for $$anonymous$$ainCamera.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do you trigger a specific collider using OnTriggerEnter? 2 Answers

Creating a 68x68 grid of trigger cubes 2 Answers

Trouble with triggers and colliders 1 Answer

multiple Objects Enter OnTriggerEnter 2 Answers

Can't click gameobject when over another trigger? 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