InvalidCastException: Cannot cast from source type to destination type. ERROR

I get the error in the title when I use the script

var projectile : Transform;
var barrel1 : Transform;
var barrel2 : Transform;
var force = 100;
var canFire = boolean;
var turret : Transform;

var fireRate = .01;

function Update () {
	canFire = turret.gameObject.GetComponent("Turret").on;
	
	if (canFire == true) {
		if (Input.GetButton("Fire1")) {
			SendMessage("fire");
		}
	}
}

function fire () {
	var projectile1 = Instantiate(projectile, barrel1.position, barrel1.rotation);
	projectile1.rigidbody.AddForce(transform.up * force);
	yield WaitForSeconds(fireRate);
	var projectile2 = Instantiate(projectile, barrel2.position, barrel2.rotation);
	projectile2.rigidbody.AddForce(transform.up * force);
	yield WaitForSeconds(fireRate);
}

to access the variable on, which is a boolean, from the script

var Player : Transform;
var playerCam : Transform;
var canGetOn : boolean;
static var on : boolean;
var turretPoint : Transform;
var turretCam : Transform;

function Awake () {
	if (Player == null  GameObject.FindWithTag("Player"))
	Player = GameObject.FindWithTag("Player").transform;
	
	if (playerCam == null  GameObject.FindWithTag("MainCamera"))
	playerCam = GameObject.FindWithTag("MainCamera").transform;
	
	turretCam.camera.enabled = false;
}

function Update () {
	if (Vector3.Distance(turretPoint.position, Player.position) > 1) {
		canGetOn = false;
	}
	
	else if (Vector3.Distance(turretPoint.position, Player.position) < 1) {
		canGetOn = true;
	}

	
	if (on == true) {
		Player.position = turretPoint.position;
		Player.GetComponent(CharacterMotor).enabled = false;
		Player.GetComponent(MouseLook).enabled = false;
		playerCam.GetComponent(MouseLook).enabled = false;
		turretCam.camera.enabled = true;
		playerCam.camera.enabled = false;
		transform.GetComponent(MouseLook).sensitivityX = 5;
		
		if (Input.GetButtonDown("Getin")) {
			on = false;
		}
	}
	
	else if (on == false) {
		Player.GetComponent(CharacterMotor).enabled = true;
		Player.GetComponent(MouseLook).enabled = true;
		playerCam.GetComponent(MouseLook).enabled = true;
		turretCam.camera.enabled = false;
		playerCam.camera.enabled = true;
		transform.GetComponent(MouseLook).sensitivityX = 0;
		
			
		if (canGetOn == true) {
			if (Input.GetButtonDown("Getin")) {
				on = true;
			}
		}
	}

}

function OnGUI () {
	if (canGetOn == true  Screen.lockCursor == true  on == false) {
		GUI.Box (Rect (Screen.width / 2 + 60, Screen.height / 2, 105, 30), "Press E to get on");
	}
	else if (on == true  Screen.lockCursor == true) {
		GUI.Box (Rect (Screen.width / 2 + 60, Screen.height / 2, 115, 30), "Press E to get off");
	}
}

Line number?

You’re trying to assign one type to an incompatible type, for example maybe you’re trying to say

var r : Rigidbody;
r = gameObject;

Im trying to make a Boolean equal to a Boolean. here is the complete error

you are trying to make a boolean equal a transform. your turret is declared as a transform.

no, im trying to make a boolean equal to a boolean that is in another script.

oh i see. you should make the boolean in the turret script a static variable. then you can just type canfire = turretscript.on

it is static, and i am trying what you said, but its not working

its giving me the error

can u post the code again?

ok,

var projectile : Transform;
var barrel1 : Transform;
var barrel2 : Transform;
var force = 100;
var canFire = boolean;

var fireRate = .01;

function Update () {
	canFire = Turret.on;
	
	if (canFire == true) {
		if (Input.GetButton("Fire1")) {
			SendMessage("fire");
		}
	}
}

function fire () {
	var projectile1 = Instantiate(projectile, barrel1.position, barrel1.rotation);
	projectile1.rigidbody.AddForce(transform.up * force);
	yield WaitForSeconds(fireRate);
	var projectile2 = Instantiate(projectile, barrel2.position, barrel2.rotation);
	projectile2.rigidbody.AddForce(transform.up * force);
	yield WaitForSeconds(fireRate);
}

(TurretFire)

var Player : Transform;
var playerCam : Transform;
var canGetOn : boolean;
static var on : boolean;
var turretPoint : Transform;
var turretCam : Transform;

function Awake () {
	if (Player == null  GameObject.FindWithTag("Player"))
	Player = GameObject.FindWithTag("Player").transform;
	
	if (playerCam == null  GameObject.FindWithTag("MainCamera"))
	playerCam = GameObject.FindWithTag("MainCamera").transform;
	
	turretCam.camera.enabled = false;
}

function Update () {
	if (Vector3.Distance(turretPoint.position, Player.position) > 1) {
		canGetOn = false;
	}
	
	else if (Vector3.Distance(turretPoint.position, Player.position) < 1) {
		canGetOn = true;
	}

	
	if (on == true) {
		Player.position = turretPoint.position;
		Player.GetComponent(CharacterMotor).enabled = false;
		Player.GetComponent(MouseLook).enabled = false;
		playerCam.GetComponent(MouseLook).enabled = false;
		turretCam.camera.enabled = true;
		playerCam.camera.enabled = false;
		transform.GetComponent(MouseLook).sensitivityX = 5;
		
		if (Input.GetButtonDown("Getin")) {
			on = false;
		}
	}
	
	else if (on == false) {
		Player.GetComponent(CharacterMotor).enabled = true;
		Player.GetComponent(MouseLook).enabled = true;
		playerCam.GetComponent(MouseLook).enabled = true;
		turretCam.camera.enabled = false;
		playerCam.camera.enabled = true;
		transform.GetComponent(MouseLook).sensitivityX = 0;
		
			
		if (canGetOn == true) {
			if (Input.GetButtonDown("Getin")) {
				on = true;
			}
		}
	}

}

function OnGUI () {
	if (canGetOn == true  Screen.lockCursor == true  on == false) {
		GUI.Box (Rect (Screen.width / 2 + 60, Screen.height / 2, 105, 30), "Press E to get on");
	}
	else if (on == true  Screen.lockCursor == true) {
		GUI.Box (Rect (Screen.width / 2 + 60, Screen.height / 2, 115, 30), "Press E to get off");
	}
}

(Turret)

try setting on to true or false in the awake function and see if that works

for which script?

second one. the on bool

I’m not sure why it won’t let you do that, but I don’t see why you need to create the canFire var in that script anyway… just do this:

function Update () {	
	if (turret.gameObject.GetComponent("Turret").on == true) {
		if (Input.GetButton("Fire1")) {
			SendMessage("fire");
		}
	}
}

Either way, you’re using GetComponent in Update… using GetComponent and putting it in a var right before a conditional is not going to be any faster than just using GetComponent right in the conditional.

I solved it, i just made var canFire : boolean; into var canFire = false

Static variables don’t exist for instances of a script, they exist for the script itself.

You need to use ScriptName.staticVariableName instead of getting an instance of the script.
(Turret.on instead of GetComponent blah)

I think your problem was because instead of saying var canFire : boolean;
you were saying var canFire = boolean;

Though I’m not much of a javascripter, so I could be mistaken.

So I’m getting this error as well, for no apparent reason:

private var emitters : ParticleEmitter[];
private var spawnTime : float;

function Awake () 
{
	spawnTime = Time.time;
	emitters = GetComponentsInChildren(ParticleEmitter);
}

Indicating the error at emitters = getcomponents etc. Any ideas?

Edit: nevermind, solved the problem, it didn’t like how my functions were arranged, apparently I had to wait until the function that used the emitters variable to declare it rather than immediately.

Yeah, I get the same error, but with renderer - color relations.

Error :

Code :

var things : Renderer[];
var color = Color.red;

function Update () {
	for (var object in player1)
		player1[object].material.color = Color.red;
}

(That’s not actually the code I’m using, but the entire script is really big - the whole one’s a modified version of the Pause Menu from the wiki, allowing color customization.)
This error only occurs when the script is executed, never in the editor. :face_with_spiral_eyes: