I’m using c# 2 scripts. To make it easy: script1.cs and script2.cs.
I want to use the value of a integer from script1 in script2.
script1:
public class script1 : MonoBehaviour {
public int ainteger;
// Use this for initialization
void Start () {
}
void Update(){
if (Input.GetKeyDown("f")) {
ainteger = ainteger -1;
}
}
How can I acces ainteger from script2?
in script 1
script2 oScript2;
void Start()
{
oScript2 = GetComponent(typeof(script2)) as script2;
}
void Update()
{
int script2val = oScript2.ainteger;
}
edit : corrected scriptnames
Since you’re using C#, you may as well take advantage of generics to avoid runtime casts:
oScript1 = GetComponent();
I’m assuming script2 has the wrapping class tags and extends MonoBehaviour, and both scripts are on the same object? If so, I think that should work. What error are you getting?
I did not got a error, I just did not know how to do it.
Thanks!! I know it’s a basic, but I just could’n figure out how to use it.
Again: thanks.