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 Venatal · Nov 18, 2015 at 04:45 AM · c# tutorial

Scoring system help

I want to create a scoring system so that every time a set color collides with another set color you get one point and the colorObject is destroyed i.e blue ball and a blue wall however I created the scripts for gameobject(destroy) for each color in 4 different scripts so I was wondering if it is possible that if a blue ball collides with a blue wall in the if statement I could write successfulCollision; and then in my score script I could use some kind of code to add a point for every successfulCollision;

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 Venatal · Nov 18, 2015 at 04:46 AM 0
Share

example of one of the collision scripts: using UnityEngine; using System.Collections;

 public class RedSquareCollision : $$anonymous$$onoBehaviour {
 
     public AudioClip[] audioClip;
     private string SuccessfulCollision;
 
     void OnCollisionEnter2D(Collision2D collision2D)
     {
        
         if (collision2D.transform.name == "RedWall")
         {
             PlaySound(0);
             Destroy(gameObject);
             SuccessfulCollision;
         }
 
         if (collision2D.transform.name == "BlueWall")
         {
             PlaySound(1);
         }
 
         if (collision2D.transform.name == "GreenWall")
         {
             PlaySound(1);
         }
 
         if (collision2D.transform.name == "YellowWall")
         {
             PlaySound(1);
         }
 
     }
 
 
     void PlaySound(int clip)
     {
         AudioSource audio = GetComponent<AudioSource>();
         audio.clip = audioClip[clip];
         audio.Play();
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ZefanS · Nov 18, 2015 at 06:38 AM

So right now it looks like your script detects when the correctly coloured ball hits the correctly coloured wall, so then you just need a script to keep track of the score, with a method that can be called to increase it. Something like:

 using UnityEngine;
 using System.Collections;

 public class ScoreKeeper :  MonoBehaviour
 {
     public int score;
 
     void Start()
     {
         score = 0;
     }
 
     public void IncreaseScore()
     {
         score++;
     }
 }

Attach this to a score-keeping GameObject and create references to the script in each of your collision scripts. Then you can do this:

  void OnCollisionEnter2D(Collision2D collision2D)
  {
      if (collision2D.transform.name == "RedWall")
      {
          PlaySound(0);
          Destroy(gameObject);
          scoreKeeper.IncreaseScore();
      }
           //...
  }

Hope this helps.

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 Venatal · Nov 18, 2015 at 02:05 PM 0
Share

Why do I get this error? NullReferenceException: Object reference not set to an instance of an object This is my code : using UnityEngine; using System.Collections;

 public class BlueSquareCollision : $$anonymous$$onoBehaviour {
 
     public AudioClip[] audioClip;
 
 
     void OnCollisionEnter2D(Collision2D collision2D)
     {
 
         if (collision2D.transform.name == "BlueWall")
         {           
             ScoreSystem scoreSystem = GetComponent<ScoreSystem>();
             scoreSystem.IncreaseScore();
 
             Destroy(gameObject);       
         }
 
         if (collision2D.transform.name == "RedWall")
         {
             PlaySound(1);
         }
 
         if (collision2D.transform.name == "GreenWall")
         {
             PlaySound(1);
         }
 
         if (collision2D.transform.name == "YellowWall")
         {
             PlaySound(1);
         }
 
        }
 
 
     void PlaySound(int clip)
     {
         AudioSource audio = GetComponent<AudioSource>();
         audio.clip = audioClip[clip];
         audio.Play();
     }
 
 }
 
avatar image ZefanS · Nov 19, 2015 at 12:32 AM 1
Share

In general that error means that the script can't find the Object being referenced to. For instance, if my script had something like:

 public GameObject myGameObject;
 
 myGameObject.GetComponent<$$anonymous$$yScript>().$$anonymous$$y$$anonymous$$ethod();

And I forgot to assign a GameObject to myGameObject in the Inspector, I would get that NullReferenceException error.

What line does it list as the source of the error? I'm going to guess that

 ScoreSystem scoreSystem = GetComponent<ScoreSystem>();

isn't working correctly. Did you attach the ScoreSystem script to the same GameObject as the BlueSquareCollision script? If not, you need to specifiy which GameObject you are getting the component from.

 ScoreSystem scoreSystem = myGameObject.GetComponent<ScoreSystem>();

and assign myGameObject as a variable in the Inspector or otherwise get a reference to it.

Or do something like this in-line:

 ScoreSystem scoreSystem = GameObject.Find("$$anonymous$$yScoreGameObject").GetComponent<ScoreSystem>();

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

35 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

Related Questions

Why is this not working? 1 Answer

Enemy destroys player if scale is bigger 1 Answer

Saving data in a txt file from Input Field button? 0 Answers

I m getting error please solve my errors I m beginner in unity,Getting error of : 2 Answers

unity c# money system 2 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