- Home /
Pickup Weapons with raycast? JS
Ok, so I have set up a system where when the player steps over an object, it raycasts down and if there's something there a gui is brought up and the player should be able to press e to pick up the weapon. But, every time i press e on the weapon I get this error:
NullReferenceException: Object reference not set to an instance of an object EquippedWeapons.Update () (at Assets/EquippedWeapons.js:38
I've attached a script called EquippedWeapons to the player who has a child that is the camera and listener which then has a child for all of the weapons in the game under that camera. The system I'm going with for now is to have every weapon available but can only be set to active if the player picks it up. WeaponPickup is the script I've attached to the weapon on the ground which contains the variable this weapon which is a game object. I defaulted weapon1 to be a pistol and weapon two is an assault rifle. the weapon I'm picking up currently is also a pistol which may be what's presenting problems because of the activating and deactivating in the hierarchy system I took for switching. The script should explain it better than I can, but my point is that every time I try to pick up a weapon it pauses and gives me that error message. I think there's some flaw in logic of some sort but I'm not sure. I'm not married to this system though of picking up weapons, so if anybody has a suggestion on a better overall strategy to handle the picking up and equipping of weapons I'd consider that as well.
var weapon1 : GameObject;
var weapon2: GameObject;
var distance : float;
function Start() {
weapon1.SetActive (true);
weapon2.SetActive (false);
}
function Update () {
//changes active weapon
if (Input.GetKeyDown(KeyCode.Tab)) {
swapWeapons();
}
//pickup weapons scripting
var hit : RaycastHit;
//raycast to find pickup-able weapon, and displays appropriate GUI prompt if so
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.down), hit)){
distance = hit.distance;
if (distance <= 1.5) {
hit.transform.SendMessage("showGui", SendMessageOptions.DontRequireReceiver);
//sets weapon 1 or two equal to picked up weapon depending on which is active. Problem lies somewhere in here I believe
if (Input.GetKeyDown(KeyCode.E))
{
Debug.Log ("ButtonPressed");
if (weapon1.activeInHierarchy == true)
{
weapon1 = hit.transform.GetComponent.<WeaponPickup>().thisWeapon;
}
if (weapon2.activeInHierarchy == true)
{
weapon2 = hit.transform.GetComponent.<WeaponPickup>().thisWeapon;
}
}
}
}
}
function swapWeapons ()
{
if (weapon1.activeInHierarchy == true) {
weapon1.SetActive (false);
weapon2.SetActive (true);
} else {
weapon1.SetActive(true);
weapon2.SetActive(false);
}
}
Answer by Cherno · Jun 14, 2015 at 02:34 PM
You are trying to access a component "WeaponPickup" of the hit gameObject which it doesn't have, or the "thisWeapon" GameObject variable of the component is null.
try checking if the required component and the weapon variable in it isn't null before trying to access it.
WeaponPickup weaponPickup = hit.transform.GetComponent.<WeaponPickup>();
if(weaponPickup != null) {
GameObject thisWeapon = weaponPickup.thisWeapon;
if(thisWeapon != null) {
weapon2 = thisWeapon ;
}
}
Glad to be of help.
You might find that using OnTriggerStay() is an easier way of pinteracting with items laying on the ground.
Your answer
Follow this Question
Related Questions
Clicking on an Object to Make it the Variable Target 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Raycast Pause Between Hits? 0 Answers
Changing position of a RayCast 1 Answer
Convert c# to Js? 1 Answer