So i am trying to make a multiplayer survival game like DayZ… I have multiplayer down so far… But when i go to pick up a weapon it wont let me. It just gives me this error UnityEngine.GameObject.GetComponent[WeaponBehavior] Please someone help me. The full error is this

UnityEngine.GameObject.GetComponent[WeaponBehavior] () (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineGameObject.cs:28)

any help would be GREATLY appreciated thank you

A NullReferenceException when calling GetComponent means the component you’re trying to find doesn’t exist. Make sure the component you’re trying to access is actually attached to the GameObject from which the script calling GetComponent is running.

If the component is inside a different GameObject, you have a couple of options. You can use GameObject.Find() to find the GameObject which has WeaponBehavior in it, or you can create an inspector-exposed variable to let you drag the GameObject onto the script from inside the editor. In case you don’t know how to create a variable that’s visible and editable from within the editor, here’s how:


var WeaponControlObject : GameObject;


public GameObject WeaponControlObject;

For more information, read the manual on using GameObjects and components from scripts.