Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

exiting question for exiting idea :) please help

Discussion in 'Scripting' started by karammaks, Sep 10, 2014.

  1. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    Hi
    Im making a game and im new in unity , i tried to ask some question before but i couldn't explain my idea clearly , so now i want to do so.

    in my game i have movable box which collect some objects , some of them are Donuts , in the following picture for example , i have collected 3 donuts , which i call it 'sumDonut' , and in the bottom the total score which is 7 , what i want to do is , if the player pressed on that donut

    1-it will multiply its number with x2 .(3x2=6)
    2-and the sumDonut which is 6 , to the score which is 7 .(6+7=13)
    3-return sumDonut to 0.
    4-change the score from 7 to 13.

    this is the code i wrote and i hope you guys to help me how to reach to my goal

    in the first class

    Code (CSharp):
    1. DonutButton other = gameObject.GetComponent <DonutButton>();
    2.         if (other != null) {
    3.             other.OnMouseDown();
    4.                     sumDonut=0;                      
    5.  
    6.                 }
    7.                         scoreText.text = "Score : " + Score;
    8.                         donutScoreText.text = ": x " + sumDonut;
    the 2nd class for the DonutButton

    Code (CSharp):
    1. public class DonutButton : MonoBehaviour {
    2.     public static int scr;
    3.     public void OnMouseDown (){
    4.  
    5.         scr = DestroyObjectAndAddPoint.sumDonut * 2;              
    6.  
    7.         DestroyObjectAndAddPoint.Score=DestroyObjectAndAddPoint.Score+scr;
    8.  
    9.  
    10.                 }
    11.    
    12.  
    13. }
    please guys help me because im new in unity and i have loads of ideas i want to apply in my game

    thank you all :)
     

    Attached Files:

    • a1.jpg
      a1.jpg
      File size:
      63.1 KB
      Views:
      664
  2. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    any help ?
     
  3. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    Like this?

    Code (CSharp):
    1. //ScoreKeeper.cs
    2. //C#
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class ScoreKeeper : MonoBehaviour
    7. {
    8.     //for storing a static reference to this scoreKeeper script
    9.     private static ScoreKeeper _instance;
    10.     public static ScoreKeeper Instance { get { return _instance; } }
    11.    
    12.     //for score keeping
    13.     public int sumDonut;
    14.     public int Score;
    15.    
    16.     //text fields
    17.     public TextMesh scoreText;
    18.     public TextMesh donutScoreText;
    19.    
    20.     public void Awake()
    21.     {
    22.         //setup the static reference to this scoreKeeper script
    23.         _instance = this;
    24.     }
    25.    
    26.     public void Accumulate(DonutButton donutButton)
    27.     {
    28.         //reset score accumulator
    29.         sumDonut = 0;
    30.        
    31.         //update text fields
    32.         scoreText.text = "Score : " + Score;
    33.         donutScoreText.text = ": x " + sumDonut;
    34.     }
    35. }
    36.  
    Code (CSharp):
    1. //DonutButton.cs
    2. //C#
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class DonutButton : MonoBehaviour {
    7.     public static int scr;
    8.  
    9.     public void OnMouseDown() {
    10.         //grab the static reference to scoreKeeper
    11.         ScoreKeeper scoreKeeper = ScoreKeeper.Instance;
    12.        
    13.         //perform score calculation
    14.         scr = scoreKeeper.sumDonut * 2;
    15.         scoreKeeper.Score = scoreKeeper.Score + scr;
    16.        
    17.         //make scoreKeeper set sumDonut = 0, and update the text fields
    18.         scoreKeeper.Accumulate(this);
    19.     }
    20. }
    21.  
    Hope this helps.

    ~
    Another walk in the park.
     
  4. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    thank you very much about your effort , but i got an error in both lines
    Code (CSharp):
    1. scr = scoreKeeper.sumDonut * 2;
    2. scoreKeeper.Score = scoreKeeper.Score + scr;
    for 1st line says :Static member `DestroyObjectAndAddPoint.sumDonut' cannot be accessed with an instance reference, qualify it with a type name instead

    for 2nd line says :
    Static member `DestroyObjectAndAddPoint.Score' cannot be accessed with an instance reference, qualify it with a type name instead
     
  5. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    The scripts I posted are supposed to replace the ones you have, scoreKeeper is the DestroyObjectAndAddPoint script, and DonutButton is the DonutButton script. ScoreKeeper must be attached to an object in your game scene.
    btw if any of your other scripts reference the DestroyOjbectAndAddPoint script, you'll want to update those the same as I use it in the DonutButton script. Hope this helps!

    ~
    Another walk in the park.
     
  6. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    i replaced your class name ScoreKeeper with mine DestroyObjectAndAddPoint , and its already attached to an object , but the error says "qualify it wit a type name instead ?!?!
     
  7. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    can u please copy paste the full error message (if you select the error in the console and press ctrl+c or cmd+c) then ctrl/cmd+v it here, thx. Then I can help further.
     
  8. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    Assets/DonutButton.cs(12,35): error CS0176: Static member `DestroyObjectAndAddPoint.sumDonut' cannot be accessed with an instance reference, qualify it with a type name instead

    Assets/DonutButton.cs(13,49): error CS0176: Static member `DestroyObjectAndAddPoint.Score' cannot be accessed with an instance reference, qualify it with a type name instead
     
  9. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    Hi, you defined a singleton but that doesn't make the members static. You need to go through the singleton reference.

    Code (CSharp):
    1. ScoreKeeper.Instance.Score
    2. ScoreKeeper.Instance.sumDonut
     
  10. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    @_met44 is right; except since you renamed the class to DestroyObjectAndAddPoint, you use that so like:

    This line makes it easy to reference the script:
    DestroyObjectAndAddPoint scoreKeeper = DestroyObjectAndAddPoint.Instance;

    then you can just do scoreKeeper.Score = 3;
    or scoreKeeper.sumDonut = scoreKeeper.sumDonut * 2;

    I hope you understand what I am showing you here!
     
  11. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DonutButton : MonoBehaviour {
    5.     public static int scr;
    6.    
    7.     public void OnMouseDown() {
    8.         //grab the static reference to scoreKeeper
    9.         DestroyObjectAndAddPoint scoreKeeper = DestroyObjectAndAddPoint.Instance;
    10.        
    11.         //perform score calculation
    12.         scr = scoreKeeper.sumDonut * 2;
    13.         scoreKeeper.Score = scoreKeeper.Score + scr;
    14.        
    15.         //make scoreKeeper set sumDonut = 0, and update the text fields
    16.         scoreKeeper.Accumulate(this);
    17.     }
    18. }
    sorry can you tell what should i change in my code ? i appreciate your effort :)
     
  12. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    Code (CSharp):
    1. //DestroyObjectAndAddPoint.cs
    2. //C#
    3. using UnityEngine;
    4. using System.Collections;
    5. public class DestroyObjectAndAddPoint : MonoBehaviour
    6. {
    7.     //for storing a static reference to this scoreKeeper script
    8.     private static DestroyObjectAndAddPoint _instance;
    9.     public static DestroyObjectAndAddPoint Instance { get { return _instance; } }
    10.  
    11.     //for score keeping
    12.     public int sumDonut;
    13.     public int Score;
    14.  
    15.     //text fields
    16.     public TextMesh scoreText;
    17.     public TextMesh donutScoreText;
    18.  
    19.     public void Awake()
    20.     {
    21.         //setup the static reference to this DestroyObjectAndAddPoint script
    22.         _instance = this;
    23.     }
    24.  
    25.     public void Accumulate(DonutButton donutButton)
    26.     {
    27.         //reset score accumulator
    28.         sumDonut = 0;
    29.      
    30.         //update text fields
    31.         scoreText.text = "Score : " + Score;
    32.         donutScoreText.text = ": x " + sumDonut;
    33.     }
    34. }
    35.  
    Code (CSharp):
    1. //DonutButton.cs
    2. //C#
    3. using UnityEngine;
    4. using System.Collections;
    5. public class DonutButton : MonoBehaviour {
    6.     public static int scr;
    7.     public void OnMouseDown() {
    8.         //grab the static reference to scoreKeeper
    9.         DestroyObjectAndAddPoint scoreKeeper = DestroyObjectAndAddPoint.Instance;
    10.      
    11.         //perform score calculation
    12.         scr = scoreKeeper.sumDonut * 2;
    13.         scoreKeeper.Score = scoreKeeper.Score + scr;
    14.      
    15.         //make scoreKeeper set sumDonut = 0, and update the text fields
    16.         scoreKeeper.Accumulate(this);
    17.     }
    18. }
    19.  
    Your code looks right, but for completeness here's how both of the scripts might look! Sorry I figured this would be easier for you. It's so far proving to not be so easy as I thought.
     
  13. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    the class name is not my problem , i wonder to tell me why still this error comes and what should i change in the code to fix it :)
     
  14. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    1/
    DestroyObjectAndAddPoint => Class name
    public int sumDonut => Class member
    public static int someOtherDonut => Class static member

    2/
    DestroyObjectAndAddPoint instanciatedObjectName = DestroyObjectAndAddPoint.Instance;
    instanciatedObjectName => Object reference

    3/
    DestroyObjectAndAddPoint.Score => invalid code since "Score" isn't a static member
    instanciatedObjectName.Score => valid code since you're accessing a public member through an instance reference

    What you need is a C# tutorial ^^. I wish you best luck with your project.