Changing a variable from another script with GetComponent

Hi guys i have a problem,
i’m trying to change the value of the variable “canShoot” from a script to another.
i have a “Spaceship” object with a “Spaceship” script and i’m trying to access it.

it gives me this error:
1071176--40002--$Schermata 2012-10-28 a 02.44.20.png

the Bullet script attached to the Bullet prefab:

using UnityEngine;
using System.Collections;

public class Bullet : MonoBehaviour {
    private float speed = 23;

    void Update () {
    	transform.Translate(Vector3.forward * speed * Time.deltaTime, Space.Self); 
    }
	
	void OnTriggerEnter(Collider other){
		//destroys the bullet if a collision (!= Player) occur, then the spaceship is allowed to shoot again.
		//basically the spaceship is allowed to shoot only one bullet at the time.
		if(other.tag != "Player"){
			Spaceship spaceship = GetComponent<Spaceship>();
			spaceship.canShoot = true; //here it gives me the error (shown before) when the bullet collides with a wall.
			Destroy(gameObject); //only works if i delete the 2 lines before.
		}
	}
}

the problem could just be the syntax?

if it helps here’s the Spaceship script:

using UnityEngine;
using System.Collections;

public class Spaceship : MonoBehaviour {
	public int speed;
	public Rigidbody bullet;
	public bool canShoot = true;

	public void Update () {
		transform.Translate(Input.GetAxis("Horizontal") * speed * Time.deltaTime,0,0);
		if(Input.GetKeyDown(KeyCode.Space)  canShoot == true){
			Rigidbody bulletPrefab = Instantiate(bullet,transform.position,Quaternion.identity) as Rigidbody;
			canShoot = false;
		}
	}
}

Thanks for reading!

You are calling GetComponent on the bullet instance whch does not have the spaceship script attached to it, so it will always be null when you try to get that component. I have changed your classes as below to add a Spaceship owner reference to bullet and set it when instantiating.

using UnityEngine;
using System.Collections;

public class Bullet : MonoBehaviour {
    private float speed = 23;
    public Spaceship owner;

    void Update () {
    	transform.Translate(Vector3.forward * speed * Time.deltaTime, Space.Self); 
    }
	
	void OnTriggerEnter(Collider other){
		//destroys the bullet if a collision (!= Player) occur, then the spaceship is allowed to shoot again.
		//basically the spaceship is allowed to shoot only one bullet at the time.
		if(other.tag != "Player"){
			owner.canShoot = true; //use the owner reference
			Destroy(gameObject);
		}
	}
}
using UnityEngine;
using System.Collections;

public class Spaceship : MonoBehaviour {
	public int speed;
	public Rigidbody bullet;
	public bool canShoot = true;

	public void Update () {
		transform.Translate(Input.GetAxis("Horizontal") * speed * Time.deltaTime,0,0);
		if(Input.GetKeyDown(KeyCode.Space)  canShoot == true){
			Rigidbody bulletPrefab = Instantiate(bullet,transform.position,Quaternion.identity) as Rigidbody;
			bulletPrefab.owner = this; //set the owner so that this spaceship knows when it's allowed to shoot again
			canShoot = false;
		}
	}
}

thanks for the help but the code isn’t working, where did the GetComponent go?
with your code i’m getting this error
IMAGE

My bad, I forgot one thing in the Spaceship class. Here it is corrected:

using UnityEngine;
using System.Collections;

public class Spaceship : MonoBehaviour {
	public int speed;
	public Rigidbody bullet;
	public bool canShoot = true;

	public void Update () {
		transform.Translate(Input.GetAxis("Horizontal") * speed * Time.deltaTime,0,0);
		if(Input.GetKeyDown(KeyCode.Space)  canShoot == true){
			Rigidbody bulletPrefab = Instantiate(bullet,transform.position,Quaternion.identity) as Rigidbody;
			Bullet blt = bulletPrefab.gameObject.GetComponent<Bullet> ();
			blt.owner = this; //set the owner so that this spaceship knows when it's allowed to shoot again
			canShoot = false;
		}
	}
}

The GetComponent call in bullet was not doing what you thought it would be because Bullet objects do not have Spaceship components attached to them.
The change I made just now was to get the Bullet component of the bulletPrefab (which I had forgotten to do before) and set its owner member. This is all forum coding and untested. It could be that you need to change the type of bullet in your Spaceship class to Transform instead of RigidBody.

it works now, thanks!
this GetComponent thing is a bit overwhelming for now, i’ll try to understand it fully another day, for now i know it works :stuck_out_tongue:

You may want to check out this tutorial.