getting two results when i shouldnt be

hi guys,

so i have this problem:

i have two scripts, one is called Main.cs and the other GutterCheck.cs

im needing the following code frong Main.cs to add points to a variable in the in GutterCheck.cs

////// Main.cs

public class Main : MonoBehaviour {

private GutterCheck myGutterCheckScript;   
    
void Start(){
                 myGutterCheckScript = new GutterCheck();    
}

void Update () {
        if ( Input.GetMouseButtonDown (0)){ 
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
            if ( Physics.Raycast (ray,out hit,100.0f)) {
                if(hit.collider.tag == "enemy") { 
                    
                    print("---enemy hit");

                    myGutterCheckScript.updateScoreBoard(200);
                      
                }
            }
        }
    }
}

this is what i have in GutterCheck.cs…

/////GutterCheck//////

public class GutterCheck : MonoBehaviour {


                     
public int totalScore;
     


public void updateScoreBoard(int amount){
   totalScore += amount;  //add the amount that was passed to the score
          print ("totalScore = " +totalScore);
     }

    
}

My problem is that if i call the function from the Main.cs i get one lot of results and when i call the function from the GutterCheck i get an entirely new set of results

so for eg: when i call it from Main.cs the result will be 200, 400, 600 etc
when i call it from GutterCheck.cs i get 2, 4, 6, 8 etc

its not actually calculating it together which makes me think that it sees it as another variable or something but how do i fix this?

Cheers,

i have another idea that is it

 void Start(){
               
 }
 
 void Update () {
         if ( Input.GetMouseButtonDown (0)){ 
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
             if ( Physics.Raycast (ray,out hit,100.0f)) {
                 if(hit.collider.tag == "enemy") { 
                     
                     print("---enemy hit");
 
                     GutterCheck .totalScore+=200;
                       
                 }
             }
         }
     }
 }

and the other script

 public class GutterCheck : MonoBehaviour {
 
 
                      
 public static int totalScore;
      
 
 
 void Update()
{
 print ("totalScore = " +totalScore);
}
 
     
 }

You made a big mistakes here. MonoBehaviour scripts can not be created with “new”. Well you can, but it doesn’t work properly as MonoBehaviour derived classes are components which need to be attached to a gameobject. You should actually receive a warning / error in the console about that.

Instead of creating a new instance of your “GutterCheck” script in Start, you should simply assign the object that has this script attached to to your “myGutterCheckScript” variable in the inspector. So make sure you remove that line completely:

myGutterCheckScript = new GutterCheck(); 

ps: Even though Josh suggested using a static variable, it’s in general not a good solution. Static things only exist once and will limit the usage of your script. static variables are “global state” which should be avoided if possible as it makes scripts and programs in general hard to debug / verify. It’s better to use references between different components / classes. Those references can be set up either in the inspector at edit time or by using GameObject.Find + GetComponent at runtime.