In the challenge, I chose to access the rocket audio through the Movement component. This also eliminated the sound carry-over issue without adding the PlayOnShot.Stop. Is this going to create problems later?
GetComponent().rocketAudio.PlayOneShot(rocketCrash);
Hi @The_Doc_Martin,
Welcome to our community! It’s great to see that you tried to come up with your own solution.
Is this going to create problems later?
No, it probably won’t create any problems if you do what Rick does. However, as game developers, we are no clairvoyants. For this reason, if your code works and if you do not see any potential issues with it, don’t worry about any future problems. I’m sure, if there are any problems due to your current solution, you will be able to solve them.
I don’t know how your entire code looks since you just shared this one line, so I don’t know if the following will help you:
GetComponent is one of the slowest methods in Unity. If you look for the same objects multiple times during the lifetime of the current object (here: the Movement component), you could improve the performance of your code by caching the reference to the component (or whatever object you need) in an instance variable (= a variable at the top of your class).
Then you call GetComponent once to assign an object to the variable, and use the variable whenever you want to access the other object.
I hope this helped.
See also:
- Forum User Guides : How to apply code formatting within your post
- Forum User Guides : How to mark a topic as solved
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.