Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Outlaw Lemur · Aug 31, 2012 at 11:59 PM · c#lightread-onlygame-object

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Outlaw Lemur · Sep 01, 2012 at 12:28 AM 0
Share

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) {

avatar image aldonaletto · Sep 01, 2012 at 11:28 AM 0
Share

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; } }

avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Outlaw Lemur · Sep 01, 2012 at 12:12 AM 0
Share

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

avatar image Eric5h5 · Sep 01, 2012 at 12:18 AM 1
Share

Yes really. It's standard that class variables are by reference; structs are by value.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can I find intersection points between a circle and Colliders 2D ? 0 Answers

Turn on LED Light on iPhone using C# 2 Answers

An OS design issue: File types associated with their appropriate programs 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges