- Home /
Changing a variable from one script to another C#
hello , i know this question has been asked but i haven't got any perfect answer yet so , i have a script called "Score" which has an int variable called CurrentScore = 0
now i have another script which i want it to get that component and the variable "CurrentScore" then on a Collision even ( OnCollisionEnter ) it access the variable "CurrentScore" and changes it to 1++ so every collision it adds 1 . thanks everybody and btw im using C#
oh one more thing , i also have created another gameobject which contains a script called "GlobalTimer" which has an int variable called Timer (its a count down timer) i also want with the collision event above to add lets say (int) more 3 seconds to the timer and thanks again
so , i know how to get a component but the problem is i dont know how to get the variable and add a 1++ to it :)
Hello guys , thanks for participating and answering but finally i figured out how to do such a thing so
public static int CurrentScore = 0; //we make it static so we can access it from any script.
//from script B we simply do this.
//(your class name aka script name) assu$$anonymous$$g its called ScoreG$$anonymous$$
void OnCollisionEnter()
{
ScoreG$$anonymous$$.CurrentScore.DoStuff();
// or in my case
ScoreG$$anonymous$$.CurrentScore += 1;
}
Answer by VIPINSIRWANI · Jul 22, 2014 at 01:08 PM
For Accessing any member of class in other class you can do like that.
public class Score : MonoBehaviour {
public static Score instace;
public int CurrentScore;
// Use this for initialization
void Start () {
instace = this;
}
}
Now you Can simply access Current Score in any script like
Score.instance.CurrentScore = 10; // Or Whatever
NullReferenceException: Object reference not set to an instance of an object
It still works and is very helpful! But does it affect the fps? @VIPINSIRWANI
In the above @ VIPINSIRWANI meant to write instance on lines 2 and 6 of his first code segment
This is an example of 'singleton' design pattern. Only one instance of CurrentScore can ever exist
Answer by ironblock · Jul 24, 2014 at 07:32 AM
MoveTo MT = gameObject.GetComponent<MoveTo> ();
MT.enabled = false;
this is the prettiest way " MT" will be local this way and wont interfere with other scripts in the same class.
scriptName SN = gameObject.GetComponent<scriptName> ();
SN.var= value;
sudo code ^
Answer by YoungDeveloper · Jul 22, 2014 at 08:45 AM
I explain it very clearly in these two topics: http://answers.unity3d.com/questions/730088/transform-var-between-scripts.html
Answer by alejo9719 · Dec 18, 2016 at 09:12 PM
I simply don't know why this doesn't work in my code. I have a "Motorcycle" object with a "BackWCollider" child object which has a "MoveBike" script attached to it.
Script in "Motorcycle":
private float ThrustIncrement;;
private MoveBike MoveB;
void start()
{
MoveB = transform.Find("BackWCollider").GetComponent<MoveBike>();
}
void OnTriggerEnter(Collider coll)
{
if (coll.gameObject.name == "NitroModel")
{
ThrustIncrement = 0.1f * MoveB.Thrust;
}
}
Script in "BackWCollider" (MoveBIke script):
public class MoveBike : MonoBehaviour {
public float Thrust; //Wheel torque multiplier
...
I get this error in the ThrustIncrement setting line:
NullReferenceException: Object reference not set to an instance of an object PowerUps.OnTriggerEnter (UnityEngine.Collider coll)
Your answer
![](https://koobas.hobune.stream/wayback/20220613154658im_/https://answers.unity.com/themes/thub/images/avi.jpg)