- Home /
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,
$$anonymous$$ay I ask how you are calling it from GutterCheck.cs if you instantiate one in $$anonymous$$ain.cs?
void Start(){
myGutterCheckScript = new GutterCheck();
}
Because each instantiation of a non-static class with non-static variables will be a separate new instance of the script. They won't be connected in a any way.
Hi Allen,
Thanks for getting back,
I'm not sure that I have it set up correctly as I'm only learning how to do it.
the way I'm initialising it is like this:
public GutterCheck myGutterCheckScript; // Give access to another script
then I'm setting it up:
void Start(){
myGutterCheckScript = new GutterCheck();
}
then I'm calling it from the Update when the mouse button clicks an object that has a tag named "enemy"
void Update () {
if ( Input.Get$$anonymous$$ouseButtonDown (0)){
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if ( Physics.Raycast (ray,out hit,100.0f)) {
if(hit.collider.tag == "enemy") {
print("---$$anonymous$$ole hit");
myGutterCheckScript.updateScoreBoard(200);
}
}
}
}
how can I write it that it can link to the same variable?
thanks :)
There are a few ways to do this.
One way is to make the variable a static one:
public static int totalScore;
This way, any instance of the script will have the same value for totalScore.
Answer by Bunny83 · Jan 09, 2017 at 05:16 AM
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.
Answer by josh15 · Jan 08, 2017 at 08:28 AM
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);
}
}
oh dude! you're a legend! thank you so much that worked! argghhh! so many hours wasted banging my head and it all came down to one word ... "static" lol
Your answer
Follow this Question
Related Questions
how to access variables in a function script attached to a clone 1 Answer
problems accessing variables and functions in another script 1 Answer
Referncing variables such as OnTriggerEnter(Collider other) thumb rule? 1 Answer
Can I pass a variable to a function to be assigned in c# 2 Answers
Referencing functions from other scripts as variables 4 Answers