Accessing variable from other script.

I’m trying to make a slender game. I want to that when i look less sanity = more intense noise effect. But it’s not working yet. How would i access the sanity variable from my slenderAI script?


using UnityEngine;
using System.Collections;

public class slenderAI : MonoBehaviour {

public Transform player;
public float timer = 20.0f;
public static float sanity = 100;

public AudioClip Static;



using UnityEngine;
using System.Collections;

public class StaticScreen : MonoBehaviour {
public float sanity;
void Update(){
	if(sanity < 50)
		Debug.Log ("Test");

slenderAI.sanity should point at it, but if you’re wanting a global variables class, the class itself and all its members should be static. Otherwise, you should pass around instance variables or assign them to public properties in the inspector.

Unity Gems has a good tutorial in script interaction.

When hand coding directX9, to access a variable in a different script you must declare it in both scripts.

The script you reference from the variable is
public int sanity;

the script you peek at sanity through you would declare the variable again, but as an extern
public extern int sanity;