Accessing a variable from one script in another with Unity (87588)

I have two scripts ;

My first script is shipupgrade;

var shipupgrade : int = 1;

function Start () {

}

function Update () {

}

and here is my second script script name : destroy;

function Start () {}
        
function Update () {
  if(Input.GetKeyDown("u")) 
    if(gameObject.GetComponent(shipupgrade).shipupgrade == 1) {
    gameObject.GetComponent(shipupgrade).shipupgrade += 1;
    Destroy(this.gameObject);
  }
}

this doesnt work why ? help me please it says Object reference not set to an istance of an object so what i must write ? thanks :slight_smile:

they are not in same object

2 Answers

2

Do you have Shipupgrade script attached to a gameobject?

Any chance you can share how you saved just the x velocity?

I will give you my exapmle, in my case script I am accesing is attached to another game object

private TraktorSkrypt traktor;  //traktorskrypt is the name of other skrypt/class attached to gameobject named Traktor

void Awake ()
	{
		// Setting up references.
		traktor = GameObject.Find("Traktor").GetComponent<TraktorSkrypt>();
}

other method...

traktor.Flip(); //this examples call a method, you can access variables in the same way.

If it is in the same Game object you simply skip the find part. So in your case you basically need to change () to <>()

no it didnt help me but thanks they are not in same object :)