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. Dismiss Notice

why does the range variable not work with Physics.Raycast in my script

Discussion in 'Scripting' started by K_O_N_S_T_Y, Nov 20, 2020.

  1. K_O_N_S_T_Y

    K_O_N_S_T_Y

    Joined:
    Feb 24, 2019
    Posts:
    50
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class interaction : MonoBehaviour
    6. {
    7.     public Camera fpsCam;
    8.  
    9.     public float range = 10;
    10.  
    11.  
    12.  
    13.     private void Update()
    14.     {
    15.    
    16.         if(Input.GetKey(KeyCode.E))
    17.         {
    18.             RaycastHit hit;
    19.             if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
    20.             {
    21.                 if (hit.transform.name == "Ammo")
    22.                 {
    23.                     AmmoBox ammBox = hit.transform.gameObject.GetComponent<AmmoBox>();
    24.  
    25.                     ammBox.Ammo(10);
    26.                     Destroy(hit.transform.gameObject);
    27.  
    28.                  
    29.                      
    30.                     Debug.Log(Gameplay.ammo + " : Ammo Update");
    31.                 }
    32.             }
    33.         }
    34.          
    35.      
    36.      
    37.          
    38.  
    39.      
    40.     }
    41. }
    42.  
     
  2. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    Define "not work".

    What is your evidence that it's not working? What is the expected behavior and what are you actually seeing? Have you tried using Debug.Log to ensure that your range value is what you think it is?
     
    Bunny83 likes this.
  3. K_O_N_S_T_Y

    K_O_N_S_T_Y

    Joined:
    Feb 24, 2019
    Posts:
    50
    the proof is that i can pick up my ammo box no matter how far away it is
     
  4. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    And what's your "range" value? (I see that it's 10 in the script, but because it's public, it may have a different value in the inspector)
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Try using named arguments. Physics.Raycast is a HORRIBLY overloaded function, with integer (the layer mask) and floating point (maxDistance) variables that can easily be inadvertently reversed.
     
  6. K_O_N_S_T_Y

    K_O_N_S_T_Y

    Joined:
    Feb 24, 2019
    Posts:
    50
    the value works on private i dont know why the public value is not different from the private
     
  7. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Because when it's public, the value is set in the inspector, not in the script.
    If you make the value public again, check what it is set to in the inspector.
     
    Bunny83 and Joe-Censored like this.
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    This ^^^
    When you set it public, it is serialized by Unity. The value set in your script is then just treated as a default value, which will be overridden by whatever value is set in the inspector. So changing this default value doesn't actually change the value in the inspector for any existing instances of the script. Only when you add the script fresh to a GameObject will you see it set to the default values again.
     
    Bunny83 likes this.