Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Trouble having interactable objects (FPS)

Discussion in 'Getting Started' started by Ragnagna, May 4, 2024.

  1. Ragnagna

    Ragnagna

    Joined:
    Apr 28, 2024
    Posts:
    15
    Hello,

    I am a beginner working with unity 2022.3.26f. I am following a tutorial to have interactable :


    (btw I recommend the playlist of this YT it is quite clear and well guided)

    I have followed every steps since the first of its video successfully and i am now trying to make an interactable object myself, a simple ON/OFF button that emits light when ON.

    I have spent many hours trying to understand why it does not work... maybe you can help me:

    Here is my script for the "button" and a screenshot of my unity inspector.

    using UnityEngine;
    public class ButtonInteraction : Interactable
    {
    [SerializeField] private GameObject onState;
    [SerializeField] private GameObject offState;
    [SerializeField] private AudioClip onSound;
    [SerializeField] private AudioClip offSound;
    private bool isOn = false;
    private AudioSource audioSource;

    private void Start()
    {
    audioSource = GetComponent<AudioSource>();
    if (audioSource == null)
    {
    Debug.LogWarning("AudioSource component not found on the button GameObject.");
    }
    }

    public override void OnInteract()
    {
    ToggleButton();
    }

    private void ToggleButton()
    {
    isOn = !isOn;
    if (onState == null || offState == null)
    {
    Debug.LogWarning("ON or OFF state GameObjects are not assigned.");
    return;
    }

    onState.SetActive(isOn);
    offState.SetActive(!isOn);
    PlaySound(isOn ? onSound : offSound);
    }

    private void PlaySound(AudioClip clip)
    {
    if (audioSource != null && clip != null)
    {
    audioSource.PlayOneShot(clip);
    }
    else
    {
    Debug.LogWarning("AudioSource or AudioClip is not assigned.");
    }
    }
    }




    As you can see in my screenshot:

    - in the console left down corner it seems the raycast system an interaction works correctly from my character, - - but still button does not work (does not switch ON / OFF)
    - my gameobject button has a box collider
    - It has been set to my interactable layer defined in my interactable script
    - I have added my interactable and my button script to it
    - I have added my ON and OFF gameobjects to the states in the inspector of my button script

    I can t understand why it still does not work....

    There might be one thing, I was not sure I needed to have my interactable script abstract or not ?

    Here is my current interactable script:


    using UnityEngine;
    public class ButtonInteraction : Interactable
    {
    [SerializeField] private GameObject onState;
    [SerializeField] private GameObject offState;
    [SerializeField] private AudioClip onSound;
    [SerializeField] private AudioClip offSound;
    private bool isOn = false;
    private AudioSource audioSource;

    private void Start()
    {
    audioSource = GetComponent<AudioSource>();
    if (audioSource == null)
    {
    Debug.LogWarning("AudioSource component not found on the button GameObject.");
    }
    }

    public override void OnInteract()
    {
    ToggleButton();
    }

    private void ToggleButton()
    {
    isOn = !isOn;
    if (onState == null || offState == null)
    {
    Debug.LogWarning("ON or OFF state GameObjects are not assigned.");
    return;
    }

    onState.SetActive(isOn);
    offState.SetActive(!isOn);
    PlaySound(isOn ? onSound : offSound);
    }

    private void PlaySound(AudioClip clip)
    {
    if (audioSource != null && clip != null)
    {
    audioSource.PlayOneShot(clip);
    }
    else
    {
    Debug.LogWarning("AudioSource or AudioClip is not assigned.");
    }
    }
    }



    And before I had this it was abstract but I did not understood because I could not add it to my button gameobject, Can someone explain me this?


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public abstract class Interactable : MonoBehaviour
    {
    public virtual void Awake()
    {
    gameObject.layer = 7;
    }

    public abstract void OnInteract();

    public abstract void OnFocus();

    public abstract void OnLoseFocus();

    }

    Thank you a lot for your help....!
     

    Attached Files:

  2. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    136