• 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
Question by sam111 · Oct 04, 2013 at 08:18 AM · c#score systemgamepad

how to make score system ?

hello

I am making game about balls when player click on it turn from to yellow and red,i want to give player score when he make the balls turn to yellow and red,so i made gameobject and name it as score and wrote t$$anonymous$$s script

 using UnityEngine;
 using System.Collections;
 
 public class score : MonoBehaviour {
     
     
     public int scorepoint;
     
 }

then i wrote t$$anonymous$$s script

 using UnityEngine;
 using System.Collections;
 
 public class oo : MonoBehaviour {
 
 // put the first material here.
 public Material logo;
 // put the second material here.
 public Material material2;
 // put the t$$anonymous$$rd material here.
 public Material material3;
     
 public GameObject score;
     
     void start ()
     {
         score = GameObject.Find("score");
     }
  
 bool FirstMaterial = true;
 bool SecondndMaterial = false;
 bool SecondndMaterial3 = false;
 void Start () 
 {
     renderer.material =  material2;
 }
  
 void OnMouseDown()
 {
     if (FirstMaterial)
     {
         renderer.material = material2;
         SecondndMaterial = true;
         FirstMaterial = false;
         SecondndMaterial3 = false;
     }
  
     else if(SecondndMaterial)
 {
             
             var scorescript = score.GetComponent ("score")as score ;
             
             scorescript.scorepoint += Random.Range(5,10);
             
     renderer.material = logo;
     FirstMaterial = false;
     SecondndMaterial = false;
    SecondndMaterial3 = true;
     }
         else if(SecondndMaterial3)
     {
             var scorescript = score.GetComponent ("score") as  score;
             
             scorescript.scorepoint += Random.Range(10,20);
             
         renderer.material =material3 ;
         SecondndMaterial3 = false;
         FirstMaterial = true;
         SecondndMaterial = false;
         
 }
 }
 }

but when i play the game error message --> Unassigned Reference Exception: The variable score of 'oo' has not been assigned.

thanks

Comment

People who like this

0 Show 0
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

Answer by hamstar · Oct 04, 2013 at 09:53 AM

What I t$$anonymous$$nk you probably mean to do is create a score object in the "oo" script, rather than referencing a game object called "score".

You could do t$$anonymous$$s by changing "`public GameObject score;`" to:

 public score score = new score();

Then remove t$$anonymous$$s line:

 score = GameObject.Find("score");

p.s. it's common to name class names in CamelCase.

However, do you intent to add more functionality to the Score class? If not you could delete it completely and just declare the variable in you "oo" behaviour class.

Comment

People who like this

0 Show 8 · 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 sam111 · Oct 04, 2013 at 11:39 AM -1
Share

it does not work

avatar image hamstar sam111 · Oct 04, 2013 at 11:46 AM 0
Share

OK I miss-read the error. Do you have a game object named "score"?

avatar image hamstar sam111 · Oct 04, 2013 at 11:56 AM 0
Share

I have edited my answer.

avatar image clunk47 sam111 · Oct 05, 2013 at 01:06 AM 3
Share

@Dismortus, no need to thumbs down someone that is new, just let them know they need to convert to comment. You can see by him having only 43 Karma, he may not know how UA works quite yet, and that's why UA is here, to help. I'll convert to comment, perhaps you could be so kind to reverse downvote. I understand if you don't though, since "It does not work" is one of my least favorite remarks (lol)

Show more comments
avatar image clunk47 · Oct 05, 2013 at 01:09 AM 0
Share

It's not really a good thing to name a variable after a class. Just sayin'.

 public score scoreInst;
avatar image hamstar · Oct 05, 2013 at 09:36 AM 0
Share

@clunk47 Yes I realise, I was trying to match the posters variable names. My answer doesn't make sense though because I hadn't realised score was derived from MonoBehaviour. I'm not sure if I should delete an answer which has comments.

avatar image clunk47 · Oct 05, 2013 at 02:33 PM 0
Share

You definitely don't have to delete, just edit ;)

avatar image

Answer by clunk47 · Oct 04, 2013 at 03:24 PM

If it tells you somet$$anonymous$$ng has not been assigned, that's because you have 'score' defined as a public GameObject in your 'oo' class. T$$anonymous$$s just means you didn't drag your object into the 'score' slot on your oo script in the editor. Since you're defining 'score' in Start(), make it a private variable instead of public.

 private GameObject score;

         //Or just GameObject score; since variables are private by default in C#
Comment
Dismortus

People who like this

1 Show 0 · 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

Answer by AndyMartin458 · Oct 04, 2013 at 04:13 PM

If you want to use GameObject.find, then that should work, though it might be slow. The problem though is how you're trying to access its member variable. Firstly, you don't really need var here. Also, do you have the score object placed in your scene? @Clunk47 had the right approach about creating a GameObject named score, then placing the score script on that GameObject, then dragging that game object onto the score slot on oo in the Inpsector. That would remove the need to do GameObject.Find. Alas, if you want to use Find, try t$$anonymous$$s to make sure the find was successful.

 //Original
 var scorescript = score.GetComponent("score") as score;

 
 //You really should do
 if(score != null) //implies that GameObject.Find worked
 {
   //Updated
   Score scorescript = score.GetComponent<Score>(); //edited for C# correctness
   scorescript.scorePoint += Random.Range(10,20);
 }
Comment
Dismortus
clunk47

People who like this

2 Show 3 · 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 Dismortus · Oct 05, 2013 at 01:02 AM 3
Share

This looks like JS and CS combined. I think in C# if you use var, it has to be local? And the way you're using GetComponent is in C#. Not nagging, just curious because I'm learning. +1

avatar image AndyMartin458 · Oct 05, 2013 at 01:09 AM 1
Share

@Dismortus Hey, good catch. yes you are correct. That version of var would have to be inside of a function. I would much prefer this non-var version though. This is assuming that the class name was renamed to uppercase Score.

 Score scorescript = score.GetComponent<Score>();
avatar image AndyMartin458 · Jan 14, 2014 at 06:51 PM 0
Share

@sam111 was this the answer? You should select an answer.

avatar image

Answer by whydoidoit · Oct 04, 2013 at 04:20 PM

Your Start function to get the component is called start not Start so it isn't running. Then you've also got a function actually called Start - I guess you should merge these two together.

Comment
clunk47
Dismortus

People who like this

2 Show 3 · 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 whydoidoit · Oct 04, 2013 at 04:21 PM 1
Share

It's really a very good idea to follow naming conventions for other things - classes should start with a capital letter or you get very confusing code that is hard to debug.

avatar image clunk47 · Oct 04, 2013 at 04:33 PM 0
Share

I bypassed the start function thinking it was a type-o lol

avatar image AndyMartin458 · Oct 04, 2013 at 05:09 PM 0
Share

If you right click the class name to refactor the name, MonoDevelop will warn you that it bypasses the convention to name a class with a lower-case letter.

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

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to check if variable is equal to range of integers 2 Answers

An OS design issue: File types associated with their appropriate programs 1 Answer

Add bonus to score 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