- Home /
How come I can't reassign a property of a game object?
I currently have a player with a light and audio for walking. I am assigning the variables like:
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
Answer by aldonaletto · Sep 01, 2012 at 12:13 AM
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
void PlayFoostep(){ // randomly select one of the step sounds: player.audio.clip = footsteps[Random.Range(0, footsteps.Length)]; // play it: player.audio.Play(); } 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.
Still doesn't work :( I am using the code:
if (currentPostition.x > lastPostition.x || currentPostition.z > lastPostition.z || currentPostition.x < lastPostition.x || currentPostition.z < lastPostition.z) { player.audio.Play (); } else if (currentPostition.x == lastPostition.x && currentPostition.z == lastPostition.z) {
This seems a complicated way to detect movement, and probably would generate a disturbing buzz sound each time the player moved - Play restarts the sound each time you call it.
A better solution would be to register the position of the last step and measure horizontal distance until it reached some specific length, then play the sound and register the step position, and so on. You could use PlayOneShot ins$$anonymous$$d of Play, because it's easier to select the audio clip:
public AudioClip[] footsteps; // assign the step sounds to this array in the Inspector public float stepSize = 0.8; // step length
Vector3 lastPos;
void Update(){ // get displacement since last step: Vector3 delta = transform.position - lastPos; delta.y = 0; // ignore vertical distance // if bigger than the step length... if (delta.magnitude >= stepSize){ // play one of the footstep sounds... audio.PlayOneShot(footsteps[Random.Range(0, footsteps.Length)]); // and update lastPos: lastPos = transform.position; } }
Answer by Eric5h5 · Sep 01, 2012 at 12:08 AM
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.
Really? Because in winforms/wpf if I assign a value, I have to reassign it to take any effect, and whenever I do something to trigger those to change, no effect takes place
Yes really. It's standard that class variables are by reference; structs are by value.