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 boxmyth · Nov 05, 2015 at 06:06 PM · c#scoring

C# Scoring Script Not Scoring

Using Unity 5.1.1f1 - I took the scoring parts of this Unity tutorial’s C# scripts and cut out the parts I didn’t need: http://unity3d.com/learn/tutorials/projects/space-shooter/counting-points?playlist=17147

I have the Counter script on an empty game object with a box collider, and the Game Controller placed on an empty game object in the hierarchy. “Score: 0” shows up where I want it on the GUI, but no counting happens OnTriggerEnter. GameController and Player are tagged correctly, box collider is set to trigger, the Score Canvas and (child) GUI Text seem okay (‘Score: 0’ shows up at runtime at least), and the Debug Log error in the Counter script does not appear in the console . Can’t figure why this isn’t working. Any help?

 using UnityEngine;
 using System.Collections;
 
 public class Counter : MonoBehaviour
 {
     public int scoreValue;
     private GameController gameController;
     
     void Start ()
     {
         GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
         if (gameControllerObject != null)
         {
             gameController = gameControllerObject.GetComponent <GameController>();
         }
         if (gameController == null) 
         {
             Debug.Log ("Cannot find 'GameController' script");
         }
     }
     
     void OnTriggerEnter(Collider other)
     {
         if (gameObject.tag == "Player") 
         {
             gameController.AddScore (scoreValue);
                 Destroy (this);
         }
     }
 }

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class GameController : MonoBehaviour
 {
     public Text scoreText;
     private int score;
     
     void Start ()
     {
         score = 0;
         UpdateScore ();
         }
     
     public void AddScore (int newScoreValue)
     {
         score += newScoreValue;
         UpdateScore ();
     }
     
     void UpdateScore ()
     {
         scoreText.text = "Score: " + score;
     }
 }
Comment
Add comment · Show 1
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 ElDo · Nov 05, 2015 at 07:10 PM 0
Share

does your Player have a Rigidbody? because OnTriggerEnter is only sent if one of the Objects has a Rigidbody as mentioned here Therefor no Rigidbody -> No Trigger

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Levithan6785 · Nov 05, 2015 at 06:30 PM

  if (gameObject.tag == "Player") 

try replacing this line with

  if (other.gameObject.tag == "Player") 

That should do it.

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 OctoMan · Nov 05, 2015 at 06:31 PM

It seems you lose the connection to the gamecontrollerobject:

do this instead and try again:

 private GameController gameController;
 private GameObject gameControllerObject;     
      void Start ()
      {
          gameControllerObject = GameObject.FindWithTag ("GameController");
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 Levithan6785 · Nov 05, 2015 at 06:51 PM 0
Share

You don't need the GameObject to gain access to another script. You could just do.

 private GameController gameController;
 
 void Awake()
 {
      gameController = GameObject.FindWithTag("Player").GetComponent<GameController>();
 }
avatar image
0

Answer by boxmyth · Nov 05, 2015 at 07:02 PM

It was just a case of missing "other" in the gameObject.tag line. Works great now! Thanks to both of you for taking the time!

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

34 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

Related Questions

How can i get a scoring system? 1 Answer

Is there a proper way of clamping from 0 to infinity (max value)? 2 Answers

Error:CS0117 How can do I code booleans with appilicationLoadLevel C# 2 Answers

Anim object with Relative Position 1 Answer

Working on dual controls using character controller 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