Hey,
i am trying to write a WeaponChange Script. So I want to assign a object to a variable but the problem is I can´t access my object´s functions.
So I am doing this:
var Primary : ak47;
var Knife : knife;
var Secondary : smith;
function knifeswitch(){
if(Input.GetKeyDown("3"))
{
Primary.inv();
Primary.shotallowed = false;
Primary.aktiv = false;
Knife.aktiv = true;
Knife.vis();
Secondary.inv();
}
}
This is working great but when I try to assign a object like this:
var Primary : GameObject;
Primary = GameObject.Find("ak47");
I get this error when I try to access a function:
Error BCE0019: ‘inv’ is not a member of ‘UnityEngine.GameObject’. (BCE0019) (Assembly-UnityScript)
GameObject
is Unity’s GameObject
. It cannot have inv
because Unity never added it. Presumably, you have inv
variable in one of your MonoBehaviour
-based scripts. Then you have attached that script to some game object. You know the name of the game object, i.e. “ak47”. So you can find the GameObject
in code. From there, you need to retrieve your actual script with inv
in it. This script is (one of the) game object’s Component
s, specifically, MonoBehaviour
-based script that you attached. Assuming Weapon
is the name/type of your MonoBehaviour
-based script with inv
:
var Primary : Weapon;
Primary = GameObject.Find("ak47").GetComponent(Weapon);
Primary.inv();
Are you calling those functions outside of the if
block?
You are declaring a variable var Primary
inside the if
block. That local variable is not present once you leave that block. So the Primary
identifier (variable) ceases to exist. You either need to declare it as a class field, or at least outside the if
block.