Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Raycast

Discussion in 'Scripting' started by emirkutlugun01, Apr 30, 2020.

  1. emirkutlugun01

    emirkutlugun01

    Joined:
    Jan 19, 2020
    Posts:
    7
    I have a inventory code, and want to pick up objects with raycast, my item has Box Collider which isTrigger option checked and Rigidbody set to kinetic, and i get no error with this code, but nothing happens.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Inventory : MonoBehaviour
    6. {
    7.  
    8.    public GameObject inventory;
    9.    private bool inventoryEnabled;
    10.    private int allSlots;
    11.    private int enabledSlots;
    12.    private GameObject[] slot;
    13.    public GameObject slotHolder;
    14.  
    15.    void Start(){
    16.        allSlots = 40;
    17.        slot = new GameObject[allSlots];
    18.        for(int i = 0 ; i<allSlots; i++){
    19.            slot[i]= slotHolder.transform.GetChild(i).gameObject;
    20.             if(slot[i].GetComponent<ItemSlot>().item==null){
    21.                 slot[i].GetComponent<ItemSlot>().empty=true;
    22.             }
    23.        }
    24.    }
    25.    void Update(){
    26.        if(Input.GetKeyDown(KeyCode.B)){
    27.            inventoryEnabled= !inventoryEnabled;
    28.        }
    29.        if(inventoryEnabled==true){
    30.            inventory.SetActive(true);
    31.        }
    32.        else{
    33.            inventory.SetActive(false);
    34.        }
    35.        if(Input.GetMouseButton(1)){
    36.            RayCastHit();
    37.        }
    38.    }
    39.    void RayCastHit(){
    40.        RaycastHit hit;
    41.        Ray ray =new Ray(transform.position,transform.forward);
    42.        if(Physics.Raycast(ray, out hit,30)){
    43.            if(hit.collider.tag=="Item" && hit.collider.isTrigger==true ){
    44.                  GameObject pickedUpItem = hit.collider.gameObject;
    45.                  Item item = pickedUpItem.GetComponent<Item>();
    46.                  AddItem(pickedUpItem,item.ID,item.type,item.description,item.icon);
    47.        }}
    48.    }
    49.  
    50.    void AddItem(GameObject pickedUpItem,int itemID,string itemType,string itemDesc,Sprite itemIcon){
    51.        for(int i = 0; i<allSlots ; i++){
    52.            if(slot[i].GetComponent<ItemSlot>().empty){
    53.                pickedUpItem.GetComponent<Item>().pickedUp=true;
    54.                 slot[i].GetComponent<ItemSlot>().item=pickedUpItem;
    55.                slot[i].GetComponent<ItemSlot>().icon=itemIcon;
    56.                 slot[i].GetComponent<ItemSlot>().ID=itemID;
    57.                 slot[i].GetComponent<ItemSlot>().type=itemType;
    58.                 slot[i].GetComponent<ItemSlot>().description=itemDesc;
    59.                 pickedUpItem.transform.parent= slot[i].transform;
    60.                 slot[i].GetComponent<ItemSlot>().UpdateSlot();
    61.            }
    62.            return;
    63.        }
    64.    }
    65. }
    66.  
    67.  
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    "Nothing happens" when? Please walk us though what you are doing, what you expected to happen, and what happens instead. It would also help if you told us what you did toverify that some parts of your code are really executing, or verifying that Raycast is hitting something. Insert some Debug.Log() statements that report about what portion is executing, and what the state of important variables are. When you have all that information it is much, much easier to diagnose and lend a hand.
     
  3. emirkutlugun01

    emirkutlugun01

    Joined:
    Jan 19, 2020
    Posts:
    7
    Right, sorry about it, i have a inventory system that works with collider, i mean when i walk trough object it automatically stores in my inventory(in scripts old version), but i want to change it,i want to store object when i press "F" key to object from a distance like 5, also cod below does not print debug log so problems related to this function i guess
    Code (CSharp):
    1. void RayCastHit(){
    2.        RaycastHit hit;
    3.        Ray ray =new Ray(transform.position,transform.forward);
    4.        if(Physics.Raycast(ray, out hit,30)){
    5.            if(hit.collider.tag=="Item" && hit.collider.isTrigger==true ){
    6. Debug.Log("Ray Hit");
    7.                  GameObject pickedUpItem = hit.collider.gameObject;
    8.                  Item item = pickedUpItem.GetComponent<Item>();
    9.                  AddItem(pickedUpItem,item.ID,item.type,item.description,item.icon);
    10.        }}
    11.    }
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    The code won't log a message if the ray you cast doesn't hit anything. Look there. Perhaps use Debug.DrawLine to draw the ray in scene (not game) view to see where the ray is going.
     
  5. emirkutlugun01

    emirkutlugun01

    Joined:
    Jan 19, 2020
    Posts:
    7
    added that with correct syntax, nothing happened
    Code (CSharp):
    1.  Debug.DrawLine(transform.position,new Vector3(20,5,-5),Color.white,2.5f);
     
  6. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Directly when entering RayCastHit, add a debug line to make sure that this method is called. That is what I mean by 'make sure that the relevant parts of your code are executed' - how do you know that RayCastHit is called? By adding a Debug.Log() and watch the log
     
  7. emirkutlugun01

    emirkutlugun01

    Joined:
    Jan 19, 2020
    Posts:
    7
    it logs hit, but doesn't log ray hit.
    Code (CSharp):
    1. d RayCastHit(){
    2.        RaycastHit hit;
    3.        Ray ray =new Ray(transform.position,transform.forward);
    4.        if(Physics.Raycast(ray, out hit,30)){
    5. Debug.Log("hit");
    6.            if(hit.collider.tag=="Item" && hit.collider.isTrigger==true ){
    7. Debug.Log("Ray Hit");
    8.                  GameObject pickedUpItem = hit.collider.gameObject;
    9.                  Item item = pickedUpItem.GetComponent<Item>();
    10.                  AddItem(pickedUpItem,item.ID,item.type,item.description,item.icon);
    11.        }}
    12.    }
     
  8. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Log which object was hit, and compare it with the object that you wanted to hit. You may find that the collider belongs to a child of the object that has the tag you are looking for and you'll have to adjust for that. Insert the following before line 6:

    Code (CSharp):
    1. Debug.Log("Object that was hit: " + hit.collider.gameObject.name);
     
  9. emirkutlugun01

    emirkutlugun01

    Joined:
    Jan 19, 2020
    Posts:
    7
    it always hit plane and that's why it's not working, thanks for a lot for teaching how to find mistakes, now i need to solve it, will be so glad if you have any idea puzzle Game.png