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 /
  • Help Room /
avatar image
1
Question by elscorcho5632 · Jun 20, 2016 at 05:05 PM · getcomponentaudiosourcegameobject.find

Trouble understanding why GameObject.Find works but declaring/initializing a GameObject and using GetComponent does not

I have two scripts. EnemyTriggerScript.cs which I want to handle collision detection and play audio from my AudioManagerScript.cs.

The code below works if I use GameObject.Find but will not work when I provide an audioManagerGameObject and do a GetComponent on it.

I have verified that my GameObjects are assigned to the respective scripts in the inspector.

The error I get is "Unassigned Reference Exception: The variable audioSource of AudioManagerScript has not been assigned. You probably need to assign the audioSource variable of the AudioManagerScript script in the inspector."

Obviously this is telling me what is wrong, however I thought that initializing my audioSource in the AudioManagerScript was doing that???

audioSource = GetComponent();

I also looked throughout the inspector and specifically at the AudioManagerGameObject and AudioManagerScript.cs which I am not sure how to assign an audio source even if I made my variable public.

I am still really new to all of Unity and C# so I understand this is probably something simple but I have been searching for hours on end trying to solve this, any help would be appreciated.

EnemyTriggersScript.cs

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 public class EnemyTriggersScript : MonoBehaviour
 {
     
     [SerializeField] private GameObject audioManagerGameObject;        
     private AudioManagerScript audioManagerScript;
             
     void Start ()
     {    
         // This is the GetComponent that is not working
         //audioManagerScript = audioManagerGameObject.GetComponent<AudioManagerScript>();
 
         // This is working
         audioManagerScript = GameObject.Find("AudioManagerGO").GetComponent<AudioManagerScript>();
        
     }
     
          
     void OnTriggerEnter2D(Collider2D collision)
     {
 
         // checking for collision with shield2
         if (collision.gameObject.name == "shield2")
         {                
             audioManagerScript.AudioPlayPlayerShieldDown();
         }
 
 }


AudioManagerScript.cs

 using UnityEngine;
 using System.Collections;
 
 public class AudioManagerScript : MonoBehaviour
 {
     [SerializeField] private GameObject audioManagerGameObject;
     [SerializeField] private AudioClip audioClipPlayerShieldDown;
 
     private AudioSource audioSource;
    
     void Start ()
     {
         audioSource = GetComponent<AudioSource>();
     }
 
 
     public void AudioPlayPlayerShieldDown()
     {
         audioSource.PlayOneShot(audioClipPlayerShieldDown, 1);
     }
 
 }






Comment
Add comment · Show 5
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 elscorcho5632 · Jun 20, 2016 at 07:48 PM 0
Share

@Thajocoth @$$anonymous$$avina

So I just started a brand new 2D Project. Created 3 cubes, gave them all rigidbody2d and box colliders. Spaced them apart top, middle, and bottom, idea is to have middle cube bound between the top and bottom cubes and play sounds when it collides then reverse direction. In the two screenshots you can see that I only exposed the two audioclips on the Audio$$anonymous$$anagerGameObject and the Audio$$anonymous$$anagerGameObject to the EnemyGameObject and it all works. However going back to my original code I cannot reproduce the desired result.

Audio$$anonymous$$anagerScript.cs

 using UnityEngine;
 using System.Collections;
 
 public class Audio$$anonymous$$anagerScript : $$anonymous$$onoBehaviour
 {
     public AudioClip audioClipOne;
     public AudioClip audioClipTwo;
     private AudioSource audio$$anonymous$$anagerScriptAudioSource;
     
     void Start ()
     {
         audio$$anonymous$$anagerScriptAudioSource = GetComponent<AudioSource>();
     }
 
     public void PlayClipOne()
     {
         audio$$anonymous$$anagerScriptAudioSource.clip = audioClipOne;
         audio$$anonymous$$anagerScriptAudioSource.Play();
     }
     
     public void PlayClipTwo()
     {
         audio$$anonymous$$anagerScriptAudioSource.PlayOneShot(audioClipTwo,1);
     }
 }
 

EnemyScript.cs

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 public class EnemyScript : $$anonymous$$onoBehaviour
 {
     private float speed = 5.0f;
     Vector3 movedir = new Vector3(0,1,0);
 
     [SerializeField] private GameObject audio$$anonymous$$anagerGameObject;
     private Audio$$anonymous$$anagerScript audio$$anonymous$$anagerScript;
     private AudioSource audioSource;
     
     void Start ()
     {
         audio$$anonymous$$anagerScript = audio$$anonymous$$anagerGameObject.GetComponent<Audio$$anonymous$$anagerScript>();
         audioSource = audio$$anonymous$$anagerGameObject.GetComponent<AudioSource>();
     }
 
     void Update()
     {
         transform.position += movedir * speed * Time.deltaTime;
     }
     
     void OnTriggerEnter2D(Collider2D wut)
     {
         if (wut.gameObject.name == "TopCube")
         {
             movedir.y = movedir.y*-1;
             audio$$anonymous$$anagerScript.PlayClipOne();
         }
 
         if (wut.gameObject.name == "BotCube")
         {
             movedir.y = movedir.y*-1;
             audio$$anonymous$$anagerScript.PlayClipTwo();
         }
     }
 
 
 }
 



alt text

esgo.png (181.3 kB)
avatar image TBruce elscorcho5632 · Jun 20, 2016 at 08:51 PM 0
Share

This is because Audio$$anonymous$$anagerScript.cs is not the same as your original post.

If you look at the very top where you first post Audio$$anonymous$$anagerScript.cs you will see these variables declared

 [SerializeField] private AudioClip audioClipPlayerShieldDown;

audioClipPlayerShieldDown is private which means it is null and is never set. So when you go access it here

 public void AudioPlayPlayerShieldDown()
 {
     audioSource.PlayOneShot(audioClipPlayerShieldDown, 1);
 }

You will get an error because audioClipPlayerShieldDown is null.

Now your new script looks like this

 public AudioClip audioClipOne;
 public AudioClip audioClipTwo;

which allows you to actually set the variables in the inspector. As long as they have been set in the inspector you will not get an error. But if you set them to None in the inspector you will.

avatar image elscorcho5632 TBruce · Jun 20, 2016 at 10:04 PM 0
Share

To test that I switched them from public to [Serialized] private and it still works...Serialized still allows me to assign them in the inspector. Which is what I had all along. I do appreciate you guys looking at this though.

 using UnityEngine;
 using System.Collections;
 
 public class Audio$$anonymous$$anagerScript : $$anonymous$$onoBehaviour
 {
     [SerializeField] private AudioClip audioClipOne;
     [SerializeField] private  AudioClip audioClipTwo;
     private AudioSource audio$$anonymous$$anagerScriptAudioSource;
     
     void Start ()
     {
         audio$$anonymous$$anagerScriptAudioSource = GetComponent<AudioSource>();
     }
 
     public void PlayClipOne()
     {
         audio$$anonymous$$anagerScriptAudioSource.clip = audioClipOne;
         audio$$anonymous$$anagerScriptAudioSource.Play();
     }
     
     public void PlayClipTwo()
     {
         audio$$anonymous$$anagerScriptAudioSource.PlayOneShot(audioClipTwo,1);
     }
 }
Show more comments
avatar image elscorcho5632 · Jun 20, 2016 at 07:52 PM 0
Share

Second Screenshot

alt text

amgo.png (170.9 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Thajocoth · Jun 20, 2016 at 05:20 PM

Wouldn't audioManagerGameObject be null in that first Start()? It's declared, but not set to anything. If it was public, you could set that in the inspector to point to the right object.

GameObject.Find() will search through ALL gameObjects in the scene to find the first one that matches what you're looking for. When possible, I recommend staying away from such searches in favor of setting the variable directly in the inspector by making it public.

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

60 People are following this question.

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

Related Questions

Argument Exception: GetComponentFastPath? built .exe of the game crashes! 3 Answers

Random audio clips from array on prefab crash 0 Answers

Changing script variable from another script doesn't change it in the original script? 0 Answers

GetComponent AudioSource not working 0 Answers

Components arent being found in script references 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