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 Teal Fawn · Mar 09, 2015 at 12:21 PM · prefabscript.roguelike

How come I cannot set my prefab game component into a script public variable?

I'm following the 2d Roguelike tutorial. In step 13 adding the sounds, I created a sound manager script in the scene. I then made it a prefab with the script and the 2 audio sources in the as a game component. I then added the SoundManager to my loader script, but I cannot drop in the prefab sound manager game component into the loader. The weird part is when I drag the prefab onto the public script variable in Loader in the Unity UI it highlights as a good link but doesn't update when the mouse is released.

My scripts are the following.

  public class Loader : MonoBehaviour
     {
         public GameManager GameManager;
 
         public SoundManager SoundManager;
 
         static Loader()
         {
             log4net.Config.BasicConfigurator.Configure(new ConsoleAppender(new SimpleLayout()));
         }
 
         private void Awake()
         {
             if (GameManager.Instance == null && this.GameManager != null)
             {
                 Instantiate(this.GameManager);
             }
             if (SoundManager.Instance == null && this.SoundManager != null)
             {
                 Instantiate(this.SoundManager);
             }
         }

 public class SoundManager :  MonoBehaviour
     {
         public static SoundManager Instance;
 
         public AudioSource EfxSource;
 
         public AudioSource MusicSource;
 
         public float LowPitchRange = .95f;
 
         public float HighPitchRange = 1.05f;
 
         public void PlaySingle(AudioClip clip)
         {
             this.EfxSource.clip = clip;
             this.EfxSource.Play();
         }
 
         public void RandomizeSfc(params AudioClip[] clips)
         {
             var randomIndex = Random.Range(0, clips.Length);
             var randomPitch = Random.Range(this.LowPitchRange, this.HighPitchRange);
             this.EfxSource.pitch = randomPitch;
             this.EfxSource.clip = clips[randomIndex];
             this.EfxSource.Pause();
         }
 
         private void Awake()
         {
             if (Instance == null)
             {
                 Instance = this;
             }
             else if (Instance != this)
             {
                 Destroy(this.gameObject);
             }
             DontDestroyOnLoad(this.gameObject);
         }
     }
Comment
Add comment · Show 1
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 _dns_ · Mar 09, 2015 at 01:47 PM 0
Share

Hi, is Sound$$anonymous$$anager nested inside Loader ? (may be only when you copy/pasted it in here). It should not : each class should be in it's own script file with the file name = class name

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dukester · Mar 09, 2015 at 01:33 PM

Not sure that is the reason, but I don't think it's a good idea to give variables the same name as the classes they represent (or any other class for that matter). So

 public GameManager GameManager;
  
 public SoundManager SoundManager;

should be:

 public GameManager gameManager;
  
 public SoundManager soundManager;





Comment
Add comment · 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

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

I have a problem whit a script and prefabs 2 Answers

Trying to have my Editor Script save the changed settings when I hit Play 0 Answers

How can i add all the prefabs in the assets directory and sub directories to List or Array ? 0 Answers

How can I apply a text variable to a prefab 2 Answers

Script on Prefab is not changeable without changing all other instaniated objects 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