- Home /
Component unassigned when it is
So i have some in my character controller code to go into my weapons manager script and grab the current weapon. That works fine, and it shows up in the editor. What is not working is that i then try and get the animator component from the current weapons anim variable, which is assigned. I then get an error when i hit play that "UnassignedReferenceException: The variable anim of Weapon has not been assigned. You probably need to assign the anim variable of the Weapon script in the inspector." the weapon script is in only my smg weapon. But it is assigned, and has no other weapon scripts in it that aren't assigned an animator component. Here is my weapon manager code and my weapon code
public class Weapon : MonoBehaviour
{
public float damage;
public float firerate;
public int ammocount;
public bool isauto;
public ParticleSystem muzzleflash;
public Animator anim;
public float reloadspeed;
}
and here is my weapon manager code
public class WeaponManager : MonoBehaviour
{
/// <summary>
/// 0 = pistol
/// 1 = smg
/// 2 = shotgun
/// </summary>
public int currentweaponnumber;
public GameObject currentweapon;
public GameObject[] weaponlist;
// Start is called before the first frame update
void Start()
{
numberintoweapon();
}
void numberintoweapon()
{
currentweapon = weaponlist[currentweaponnumber];
}
}
and here is the relevant section of my character controller script
void Start()
{
Currentweapon = this.gameObject.GetComponent<WeaponManager>().currentweapon;
anim = Currentweapon.GetComponent<Weapon>().anim;
}
why is this not working?
Your answer
Follow this Question
Related Questions
How to run a function in a GO that have DontDestroyOnLoad 1 Answer
How to Access Components in Children of GameObject? 1 Answer
Object reference not set to an instance of an object 3 Answers
Trouble accessing scripts (Boo) 1 Answer
How to get a component from an object and add it to another? (Copy components at runtime) 12 Answers