Cannot play an disabled audio source UnityEngine.AudioSource:Play()
I have seen alot of answers but none really helped me. So to start I am really a beginner, and I just wanted to make an game because i was bored. I have minimal C# experience and almost none of Unity. So for the question, I made an game with an object, that once you look at it, an audio starts playing. I have found an answer for the trigger once you look at it, even though it is really simple and maybe bad. So the title is pretty self-explanatory, everything looks fine (I think) but when I look at the object, the console writes Cannot play an disabled audio source, UnityEngine.AudioSource:Play();. And I don't know what people means by "checking the boxes" like which ones? Play on Awake? It is disabled. Here is my code to better explain:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class musicplays : MonoBehaviour
{
private Renderer render;
public new AudioSource audio;
void Start()
{
audio = GetComponent<AudioSource>();
render = gameObject.GetComponent<Renderer>();
}
void Update()
{
if (render.isVisible)
{
audio.GetComponent<AudioSource>();
audio.Play();
}
}
}
I have the code attached to my GameObject, and the errors shows when I look at it. Any help is appreciated.
Your answer
Follow this Question
Related Questions
I want the audio to play when the player nears the object, but fade away when they walk away. 0 Answers
Pick Up Sound 0 Answers
Trying to use derive pitch and db from realtime microphone input 0 Answers
Trying to make a splitscreen game, having issues with the AudioSource and distance between objects. 0 Answers
Stop AudioSource on Trigger!!! 1 Answer