- Home /
How do you access other Variables on other scripts c#
hey guys i need some help with accesing a variable from another script. here is my script. oh, and my variable i need to access is called "timer" thanks for the help. here is my script.
public class monkeymovescript : MonoBehaviour {
// Use this for initialization void Start () { } void Update () { transform.Translate (Vector3.right); //this line is probably where you acesss the variable if (timer > 3) { transform.Translate (Vector3.left); } } }
PLEASE PLEASE PLEASE Read the manual, it's in the front. And PLEASE use the Search box here.
What @davea meant is that this question is the most asked question on Unity Answers.
The point of this website is not to offer support but to create a knowledge database. So before asking always search for similar question.
oh sorry about that. I actually did look at similar questions but they did not exactly explain what i wanted to do.
Answer by mohanrao164 · Aug 21, 2012 at 06:14 AM
this is your script
public class monkeymovescript : MonoBehaviour {
void Start () { } void Update () { transform.Translate (Vector3.right);
if (timer > 3) {
transform.Translate (Vector3.left);
} } }
if u want to call timer in another script without initalization
here is the method
public class myscript: MonoBehaviour {
private monkeymovescript mms;
void Start () {
mms = (monkeymovescript )FindObjectOfType(typeof(monkeymovescript ))
}
void Update () {
if(mms.timer >3){
//your code } }
like this you access other Variables on other scripts c#
umm it comes up with this error,
Assets/monkeymovescript.cs(16,25): error CS1061: Type `monkeymovescript' does not contain a definition for `timer' and no extension method `timer' of type `monkeymovescript' could be found (are you missing a using directive or an assembly reference?)
did u declare timer in 'monkeymovescript' script. the timer should be declare public like public int timer;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Object reference not set to an instance of an object 0 Answers
A node in a childnode? 1 Answer
Distribute terrain in zones 3 Answers
how to reference a javascript variables in a c# code. 0 Answers