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
0
Question by jhon-avila · Oct 19, 2014 at 02:40 PM · javascript

Getting acces to a script instance from a GameObject

He let us say that i have a GameObject in Unity and i associated an script to this object called Mover.cs in which i defined some properties can i access through the GameObject the instance of Mover associated to it?

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
0
Best Answer

Answer by Gardosen · Oct 20, 2014 at 11:06 AM

If you always need an Audio Source in your scenes, create a initScene, place a GameObject there, and attach a SoundConroller Script to it with all necessary AudioSources (eg. Background Music and SpecialEffects).

Set this Audio Controller as a singleton, and tell on a script DontDestroyOnLoad(GameObject); this way the EmptyGameObject with the Audio COntroller will exists in all scenes, and you can grab the audiosource you need over a getter setter system and replace the audio file, and tell audioSource.play();

this way you only have two sources which you can address, and you can be sure that they are existing, because they will stay over the whole gameclient in all scenes.

Greetings

Comment
Add comment · Show 1 · 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 jhon-avila · Oct 20, 2014 at 11:08 AM 0
Share

ok a singleton... i see... thanks.

avatar image
0

Answer by Phantomized · Oct 20, 2014 at 08:52 AM

If I understand your question correctly then yes.

If you have your Mover.cs script on a GameObject as the component, and you want to access that instance from elsewhere:

 Mover moverscript = GameObject.Find ("NameOfYourGameObject").GetComponent<Mover>();

Then you can access the members inside that script(as long as they are public) e.g:

 moverscript.move();

There are many ways to get gameobjects and their components(which is an associated instance when talking about scripts) but this is one of the most simple ones.

Comment
Add comment · Show 1 · 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 jhon-avila · Oct 20, 2014 at 10:44 AM 0
Share

Hi thanks for the reply, i did something like this, now the other problem that i am facing is that using an game-object from my prefabs is ok until it comes to an audio source, there was no way i could play my audio source from an instance of my prefab game object, it kept saying that the audio source is disabled has anyone deal with this problem? $$anonymous$$y solution was that i added a game object to the scene and played it each time but i don't like this approach... What would be the right way to do it?? imagine that your game object has sound that you want play like war cry or something that is individual for each instance....

Thanks.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

smoothly transmogrify an object as it enters a circle 1 Answer

Animated load screen. What is the command to check if a scene is being loaded or the game is transitioning between scenes? 2 Answers

how well does unity work for turn-based game time-type? 3 Answers

How does/can Unity handle offline sync with backend? 2 Answers

dead rising on unity? 2 Answers


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