- Home /
UnityEngine.GameObject.GetComponent[WeaponBehavior]
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
NullReferenceException UnityEngine.GameObject.GetComponent[WeaponBehavior] () (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineGameObject.cs:28)
any help would be GREATLY appreciated thank you
Quick suggestion: Edit the post, select the error and click the "Code" button. It will stop [WeaponBehavior]
turning into a link.
Whatever it is you're trying to get a WeaponBehaviour
component from doesn't have one attached, so GetComponent
returns null
, meaning nothing. When you try to do something with nothing, you get a NullReferenceException
. You'll need to post code to get a more definitive answer and solution to your problem.
Answer by Clonkex · Apr 20, 2014 at 01:56 PM
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:
JavaScript:
var WeaponControlObject : GameObject;
C#:
public GameObject WeaponControlObject;
For more information, read the manual on using GameObjects and components from scripts.