- Home /
Question by
Tacooobell · Nov 17, 2016 at 07:20 AM ·
unity 5javascripterrorvariablesscript error
Changing a Variable in a script from another Object
Hey guys, so i'm aware that this question has been asked a dozen times, but i can't seem to wrap my head around my problem. It's to do with 'Changing a Variable in a script from another Object'. My scripts concept is that you shoot at a crate and it gives you ammunition, the script that gets changed is on the gun's front where the amount of magazines variable 'magcount' is on a script named 'bluewave_shoot.js'. The actual script 'ammobox.js' is attached onto my ammunition box gameobject.
The error i get is:
Here is my code:
//ammobox.js
var effect : Transform;
var guntoreload : GameObject;
function OnCollisionEnter(col: Collision){
if (col.gameObject.tag == "bullet"){
guntoreload.GetComponent.bluewave_shoot().magcount = 20;
Instantiate(effect, transform.position, transform.rotation);
Destroy(this.gameObject, 0.1);
}
}
This is 'bluewave_shoot.js':
//Note: this is one half of the script only (The Public Var is there)
public var magcount : int = 10;
var bullet : Rigidbody;
var power : float = 1500;
var damage : float = 100;
var reloadtime : float = 4;
var magbulletcount : int = 21;
var bulletcount : int = 0;
var otherObj : GameObject;
public var shootsound: AudioClip;
private var reloadTimer: float = 0.0;
function Start () {
bulletcount = magbulletcount;
}
function OnGUI(){
if (bulletcount <= 0 && magcount > 0){
GUI.Label(new Rect (5,20,200,20),"Reloading...");
}
else
GUI.Label(new Rect (5,20,200,20),"Bullet's Left: " + bulletcount);
GUI.Label(new Rect (5,40,200,20),"Round's Left: " + magcount);
}
function Update(){
if (reloadTimer > 0){
reloadTimer -= Time.deltaTime;
if (reloadTimer <= 0){
bulletcount = magbulletcount;
magcount--;
}
}
Hope someone can help!
Many Thanks
capture.png
(15.4 kB)
Comment
Best Answer
Answer by ForeignGod · Nov 17, 2016 at 08:02 AM
Try
guntoreload.GetComponent.<bluewave_shoot>().magcount = 20;