I currently have a player with a light and audio for walking. I am assigning the variables
static GameObject player = GameObject.FindGameObjectWithTag("Player");
AudioSource step = player.audio;
Light flashlight = player.light;
Then I change the range/mute properties and want to reassign them to the player. I am doing this by:
player.audio = step;
player.light = flashlight;
but I get the error “UnityEngine.GameObject.audio cannot be assigned to (it is read only)” Does the Update() function do this for me? Or am I doing something wrong? Thanks in advance
You don’t need to assign those back in the first place, so just leave that step out. Those variables are references, they’re not copies.
You’re positively doing something wrong: the properties audio and light are read-only, they only have the references to the corresponding components - you must instead modify the component properties in each case.
If you want to select a different sound for each step, for instance, you should do something like this:
public AudioClip footsteps; // assign the step sounds to this array in the Inspector
// randomly select one of the step sounds:
player.audio.clip = footsteps[Random.Range(0, footsteps.Length)];
// play it:
For the flashlight, you can change the appropriate properties, like color, intensity etc.
player.light.intensity = 0.5f + Random.value * 0.2f;
Take a look at AudioSource and Light to know which properties you can modify.