exiting question for exiting idea :) please help

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

DonutButton other = gameObject.GetComponent <DonutButton>();
        if (other != null) {
            other.OnMouseDown();
                    sumDonut=0;                       

                }
                        scoreText.text = "Score : " + Score;
                        donutScoreText.text = ": x " + sumDonut;

the 2nd class for the DonutButton

public class DonutButton : MonoBehaviour {
    public static int scr;
    public void OnMouseDown (){

        scr = DestroyObjectAndAddPoint.sumDonut * 2;               

        DestroyObjectAndAddPoint.Score=DestroyObjectAndAddPoint.Score+scr;


                }
   

}

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 :slight_smile:

any help ?

Like this?

//ScoreKeeper.cs
//C#
using UnityEngine;
using System.Collections;

public class ScoreKeeper : MonoBehaviour
{
    //for storing a static reference to this scoreKeeper script
    private static ScoreKeeper _instance;
    public static ScoreKeeper Instance { get { return _instance; } }
   
    //for score keeping
    public int sumDonut;
    public int Score;
   
    //text fields
    public TextMesh scoreText;
    public TextMesh donutScoreText;
   
    public void Awake()
    {
        //setup the static reference to this scoreKeeper script
        _instance = this;
    }
   
    public void Accumulate(DonutButton donutButton)
    {
        //reset score accumulator
        sumDonut = 0;
       
        //update text fields
        scoreText.text = "Score : " + Score;
        donutScoreText.text = ": x " + sumDonut;
    }
}
//DonutButton.cs
//C#
using UnityEngine;
using System.Collections;

public class DonutButton : MonoBehaviour {
    public static int scr;

    public void OnMouseDown() {
        //grab the static reference to scoreKeeper
        ScoreKeeper scoreKeeper = ScoreKeeper.Instance;
       
        //perform score calculation
        scr = scoreKeeper.sumDonut * 2;
        scoreKeeper.Score = scoreKeeper.Score + scr;
       
        //make scoreKeeper set sumDonut = 0, and update the text fields
        scoreKeeper.Accumulate(this);
    }
}

Hope this helps.

~
Another walk in the park.

thank you very much about your effort , but i got an error in both lines

scr = scoreKeeper.sumDonut * 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

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.

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 ?!?!

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.

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

Hi, you defined a singleton but that doesn’t make the members static. You need to go through the singleton reference.

ScoreKeeper.Instance.Score
ScoreKeeper.Instance.sumDonut

@_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!

using UnityEngine;
using System.Collections;

public class DonutButton : MonoBehaviour {
    public static int scr;
   
    public void OnMouseDown() {
        //grab the static reference to scoreKeeper
        DestroyObjectAndAddPoint scoreKeeper = DestroyObjectAndAddPoint.Instance;
       
        //perform score calculation
        scr = scoreKeeper.sumDonut * 2;
        scoreKeeper.Score = scoreKeeper.Score + scr;
       
        //make scoreKeeper set sumDonut = 0, and update the text fields
        scoreKeeper.Accumulate(this);
    }
}

sorry can you tell what should i change in my code ? i appreciate your effort :slight_smile:

//DestroyObjectAndAddPoint.cs
//C#
using UnityEngine;
using System.Collections;
public class DestroyObjectAndAddPoint : MonoBehaviour
{
    //for storing a static reference to this scoreKeeper script
    private static DestroyObjectAndAddPoint _instance;
    public static DestroyObjectAndAddPoint Instance { get { return _instance; } }
  
    //for score keeping
    public int sumDonut;
    public int Score;
  
    //text fields
    public TextMesh scoreText;
    public TextMesh donutScoreText;
  
    public void Awake()
    {
        //setup the static reference to this DestroyObjectAndAddPoint script
        _instance = this;
    }
  
    public void Accumulate(DonutButton donutButton)
    {
        //reset score accumulator
        sumDonut = 0;
      
        //update text fields
        scoreText.text = "Score : " + Score;
        donutScoreText.text = ": x " + sumDonut;
    }
}
//DonutButton.cs
//C#
using UnityEngine;
using System.Collections;
public class DonutButton : MonoBehaviour {
    public static int scr;
    public void OnMouseDown() {
        //grab the static reference to scoreKeeper
        DestroyObjectAndAddPoint scoreKeeper = DestroyObjectAndAddPoint.Instance;
      
        //perform score calculation
        scr = scoreKeeper.sumDonut * 2;
        scoreKeeper.Score = scoreKeeper.Score + scr;
      
        //make scoreKeeper set sumDonut = 0, and update the text fields
        scoreKeeper.Accumulate(this);
    }
}

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.

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 :slight_smile:

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.