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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Help on interacting with objects

Discussion in 'Editor & General Support' started by gregoryjimenez708, May 3, 2021.

  1. gregoryjimenez708

    gregoryjimenez708

    Joined:
    May 3, 2021
    Posts:
    2
    Hello.

    I am working on my first unity project and I came across this video guiding me on how to implement raycasts to build an interact system. However I tested the code and it didn't run. There was no errors, it just didn't execute the script.

    Please help, I am open to any suggestions as I am fairly new to unity and coding in general.

    The code I followed:

    Code (CSharp):
    1.  
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class interact : MonoBehaviour
    7. {
    8.     private GameObject raycastedObj;
    9.  
    10.     [SerializeField] private int rayLength = 10;
    11.     [SerializeField] private LayerMask layerMaskInteract;
    12.  
    13.     [SerializeField] private Image uiCrosshair;
    14.  
    15.     void update()
    16.     {
    17.         RaycastHit hit;
    18.         Vector3 fwd = transform.TransformDirection(Vector3.forward);
    19.        
    20.         if (Physics.Raycast(transform.position, fwd, out hit, rayLength, layerMaskInteract.value))
    21.         {
    22.             if (hit.collider.CompareTag("Object"))
    23.             {
    24.                 raycastedObj = hit.collider.gameObject;
    25.                 CrosshairActive();
    26.  
    27.                 if (Input.GetKeyDown("f"))
    28.                 {
    29.                     Debug.Log("I HAVE INTERACTED WITH AN OBJECT");
    30.                     raycastedObj.SetActive(false);
    31.                 }
    32.             }
    33.         }
    34.         else
    35.         {
    36.             CrosshairNormal();
    37.         }
    38.     }
    39.     void CrosshairActive()
    40.     {
    41.         uiCrosshair.color = Color.red;
    42.     }
    43.  
    44.     void CrosshairNormal()
    45.     {
    46.         uiCrosshair.color = Color.white;
    47.     }
    48. }
    49.  
    The video:
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    C# is case sensitive.

    Unity's update function is called Update, with a capital U.
     
  3. gregoryjimenez708

    gregoryjimenez708

    Joined:
    May 3, 2021
    Posts:
    2
    Wow that was it? Can't believe that slipped by me. Thanks!