- Home /
Duplicate Question
How to get a variable value from another script(C#)?
Hello, I understand the concept of getting a variable value from another script but only if there is a game object to use gameObject.GetComponent<"otherScript">(); but what if there is no game object?
This is the one that needs to be accessed:
public class PointScript : MonoBehaviour {
public int playerscore=0;
//Start()
public void OnTriggerEnter2D(Collider2D other){
if (other.tag == "Player") {
playerscore = playerscore + 1;
}
}
//Update
}
This is the one that needs to access my point script:
public class HudScript : MonoBehaviour {
private PointScript score= GetComponent<PointScript>();
float PlayerScore = score.playerscore;
//start()
//update()
}
The problem might be that I want to use the PointScript on multiple triggers.
Please reply and thanks for any help.
NB Please excuse any poor formatting, this is my first posted question.
For you to use a value of another variable should use a Statics class. However, still do not get the TYPE of value you want to get in another class. Can you explain your logic?
You absolutely should not use a static in this case. the OP should just learn how to do it properly
Answer by mattyman174 · May 02, 2014 at 11:36 PM
You want to use a Static variable in your PlayerScore class so that the variable becomes a member of the Class and not of any particular Instance of the Class you may have in your scene. You can then access that variable directly from the Class. See below.
public class PointScript : MonoBehaviour {
public static int playerScore; // Static keyword makes this variable a Member of the class, not of any particular instance.
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
playerScore++; // The code that any instance can use to cause the score to be incremented, since the playerScore variable is a Static member, all instances of this class will have access to its value regardless of what instance next updates it.
}
}
Now to access it via an outside class, all you need to do is use the Class name. As such.
public class HudScript : MonoBehaviour {
private static int score; // A new Static variable to hold our score.
void Update()
{
score = PointScript.playerScore; // Update our score continuously.
}
}
I hope this answers your question. Any problems just let me know.
I just used the following Test cases to prove that it works.
public class PointScript : $$anonymous$$onoBehaviour {
public static int playerScore; // Static keyword makes this variable a $$anonymous$$ember of the class, not of any particular instance.
void Update()
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W))
playerScore++; // Increment when i press the "W" $$anonymous$$ey.
}
}
public class HudScript : $$anonymous$$onoBehaviour {
private static int score; // A new Static variable to hold our score.
void Update()
{
score = PointScript.playerScore; // Update our score continuously.
}
void OnGUI()
{
GUI.Label(new Rect(10, 10, 200, 20), "Score: " + score); // Display the score on a label.
}
}
The more GameObjects with the PointScript attached, the more it will increase by when you hit W.
So with 2 it will increase by 2's everytime since both instances of the PointScript class have the same logic to update the playerScore variable.
@mattyman174 thx man, i was looking for this since 4 days had the same problem. static, damit, learned something new
I am seeing that on 2017 but this comment is really helpful , thanks man you save my day.
Follow this Question
Related Questions
C# GetComponent Issue 2 Answers
Accessing a value in a script attached to another game object 3 Answers
insert script question 1 Answer
Camera to follow a target within a circle? 1 Answer
Assigning current color to a variable for fade out (C#) 0 Answers