- Home /
Picking Weapons Up with Raycast
Hello, i decided to write my own pick up weapons script using a raycast route. The logic is as follows:
Tag a weapon with a new tag named after the weapon.
Place the weapon prefab in the PickUpWeapon Script.
When the PickUpWeapon script raycast hits any of the tagged weapons, and if the player presses E, then a class from Change Weapon Scrip is instantiated, and the PickedUpWeapon is put in the slot.
Unfortunately im getting a Null reference error. At the following line: cw.weapon[2] = AK; The value of Ak is the prefab assigned in this script, so logically when i call the change weapon script, the Ak should occupy slot 2.
public class PickThingsUp : MonoBehaviour
{
RaycastHit rayHit = new RaycastHit();
Ray detect = new Ray();
Vector3 Location;
public string ak;
public GameObject AK;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
detect = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
ChangeWeapon cw = GetComponent<ChangeWeapon>();
if(Physics.Raycast(detect, out rayHit, 150.0f))
{
Location = rayHit.point;
if(rayHit.collider.CompareTag("AK47"))
{
ak = "AK47";
print ("its gunz");
if(Input.GetKey(KeyCode.E))
{
cw.weapon[2] = AK;
cw.weapon[2].gameObject.SetActive(false);
print ("weapon achived");
}
}
}
}
}
public class ChangeWeapon : MonoBehaviour
{
public int currentWeapon;//current one used
public GameObject[] weapon;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
Vector3 temp = transform.position;
if(Input.GetKeyDown(KeyCode.Alpha1))
{
changeWeapon(1);
}
if(Input.GetKeyDown(KeyCode.Alpha2))
{
changeWeapon(2);
}
}
public void changeWeapon(int num)
{
currentWeapon = num;
for(int i = 1; i < weapon.Length; i++)
{
if(i == num)
weapon[i].gameObject.SetActive(true);
else
weapon[i].gameObject.SetActive(false);
}
}
}
Answer by HarshadK · May 28, 2014 at 07:49 AM
As you've said that
"The value of Ak is the prefab assigned in this script"
I don't see where you are assigning the prefab to AK in your script. And since in your PickThingsUp script the AK is not assigned with any prefab/game object it is throwing the null reference error.
The weapon array size is changed in the ChangeWeapon Script; as needed, currently i have it set to: 3. Element 0 is empty on purpose, element 1 is a revolver, the Ak should go in element 2. Is it possible that when i said element 2 i should have specified 3 or is it on, since the array starts at 0?
Got it! Since you are accessing the ChangeWeapon script like this:
ChangeWeapon cw = GetComponent<ChangeWeapon>();
It requires you to have the ChangeWeapon script on the PickThingsUp game object itself. But as per the screenshot you provided above it is not there.
If you have placed ChangeWeapon on some other game object then you need to access it like:
ChangeWeapon cw = GameObject.Find("AnotherGameObject").GetComponent<ChangeWeapon>();
Here AnotherGameObject is the name of the game object to which the ChangeWeapon script is attached.
If you have not assigned ChangeWeapon Script to any game object then you need to do so.
Yes :D! it kind of worked, i have no null error. It tells me the weapon has been achieved, but... when i press 2 to get to the Ak, it does not show up. I think, this is because i have no transform position linked. Um... here is a screen shot, Any thoughts on which script and how i should place the weaponsObject transform position?
Your answer
Follow this Question
Related Questions
raycasting in c# - NullRefrenceException error 2 Answers
AI raycasting problem 0 Answers
Object reference not set to an instance of an object 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers