Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 lefantan · Jan 18, 2017 at 03:48 PM · staticsingleton

Bug caused by Singleton Pattern

This is my audio manager script(singleton)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class AudioManager : MonoBehaviour {
 
     private static AudioManager _instance;
 
     public static AudioManager Instance {
         get { 
             if (_instance == null) {
                 Debug.Log ("No AudioManager gameobject was found");
             }
         
             return _instance;
         }
     }
 
     void Awake(){
         _instance = this;
     }
 
     void Start(){
     }
 
     public AudioSource AddAudio(GameObject targetGameObject, AudioClip clip, bool loop, bool playOnAwake, float volume){
         AudioSource newAudio = targetGameObject.AddComponent<AudioSource> ();
         newAudio.clip = clip;
         newAudio.loop = loop;
         newAudio.playOnAwake = playOnAwake;
         newAudio.volume = volume;
 
         return newAudio;
     }
 }

and this is my Shoot script(attached to enemy and player object) that calls the singleton

     void Awake(){
         shootAudio = AudioManager.Instance.AddAudio (this.gameObject, shootClip, loop, playOnAwake, audioVolume);
     }
 
        void ShootStuff(){
              shootAudio.Play();
        }

I set up up this way so that if the ShootScript(or any other script) requires another sound clip to be played, i can just create a new audio source in the script, reference it to the instance of AudioManager's AddAudio function, and a AudioSource component will be created automatically.

The problem arises when i clone the Enemy prefab, the ShootScriptComponent on the cloned Enemy automatically disables when i run the game. Why is it that the Player object that has the same shootScript attached works fine but not for the cloned Enemy, and how should i fix it??? Thanks

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 LK84 · Jan 18, 2017 at 05:03 PM 0
Share

$$anonymous$$y guess is that the local reference (this.gameObject) within the prefab is causing the problem. Have a look here for a similar problem http://answers.unity3d.com/questions/45079/instantiated-objects-scripts-not-enabled-not-sure.html

1 Reply

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

Answer by Pengocat · Jan 18, 2017 at 04:42 PM

You initialize the Singleton in Awake and you use it in Shoot Script in Awake. So you should either setup the "Script execution order" to call the singleton before the shoot or simply call the singleton in the Shoot script in Start and not Awake.

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

6 People are following this question.

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

Related Questions

Using static properties that return _instance.variable... 0 Answers

Remnant Empty Game Objects for Static Scripts 1 Answer

Having a static GameObject pointing to a prefab? 1 Answer

Don't render GameObject on load if already hit by player? 1 Answer

Why should I use a game object for non-physical things? 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