Vote count:
0
When i try to use ray cast to add to my inventory , the game run! and the script work! i think the problem is the storing of item to the inventory? anybody there, know thow to fix this? thank you in advance. i dont know the proper way of adding it. :(
public Inventory inventory;
RaycastHit hit;
GameObject pickedUpObject;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.Escape)) {
Screen.lockCursor = false;
}
else {
Screen.lockCursor = true;
}
Ray rayc = Camera.main.ViewportPointToRay (new Vector3 (0.5f, 0.5f, 0f));;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast (rayc.origin, rayc.direction,out hit, 1.5f)) {
if(Input.GetKeyDown("e")){
if(hit.collider.gameObject.tag=="GameItem"){
pickedUpObject=hit.collider.gameObject;
Destroy(pickedUpObject);
inventory.AddItem(pickedUpObject.GetComponent()); }
}
}
}
}
then i tried collider
enter code here
and it is working! but i want the raycast! :(
"NullReferenceException: Object reference not set to an instance of an object Raycasting.Update () (at Assets/Standard Assets/Items/Script/Raycasting.cs:45)"
asked 1 min ago
Unity3d AddItem with raycast
Aucun commentaire:
Enregistrer un commentaire