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 Noxury · Jan 26, 2016 at 03:21 PM · prefabaudiosourceaudioclipparametersfx

Passing an AudioClip parameter to Method

Hey,

I have the problem that I am getting NullReferenceExceptions when I want to instantiate a sfx for the weapon.

For this I use a SFX-prefab that play only its parameter-given AudioClip:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof (AudioSource))]
 public class SFX : MonoBehaviour {
 
     private AudioSource aud;
     
     void Start () {
         aud = GetComponent<AudioSource>();
         aud.spatialBlend = 1f;
     }
 
     public void selectSFX(AudioClip sound){
         aud.clip = sound; // ERROR
         aud.Play();
         Destroy(gameObject, aud.clip.length);
     }
 }

Now I instantiate it in the Weapons-class:

 GameObject sfxClone = Instantiate(Resources.Load("Prefabs/SFX"), bulletEmpty.position, transform.rotation) as GameObject;
         sfxClone.GetComponent<SFX>().selectSFX(shootSound);

but this gives me the NullReferenceException in SFX, line with //ERROR

The shootSound-variable in the weaponScript is assigned.

Thanks in advance

Comment
Add comment · Show 7
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 flashframe · Jan 26, 2016 at 05:40 PM 0
Share

Where do you declare shootSound?

avatar image Noxury flashframe · Jan 26, 2016 at 09:10 PM 0
Share

shootSound is a public variable in the weapons class and assigned by default to an existing shootSound.

avatar image brunocoimbra · Jan 26, 2016 at 05:49 PM 0
Share

The shootSound is null...

avatar image incorrect · Jan 26, 2016 at 06:15 PM 0
Share

Is your SFX actually a prefab or just a script you put in Resources/Prefabs folder?

avatar image Noxury incorrect · Jan 28, 2016 at 12:49 PM 0
Share

@incorrect No it ia a prefab that contains the sfx script and then an audiosource component.

avatar image incorrect Noxury · Jan 29, 2016 at 08:26 AM 0
Share

But I'm pretty much shure that if you add this line of code

 if(sfxClone == null) Debug.Log("sfxClone is null");

after

 GameObject sfxClone = Instantiate(Resources.Load("Prefabs/SFX"), bulletEmpty.position, transform.rotation) as GameObject;

you will see, that nothing was loaded.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
-2

Answer by lassade · Jan 26, 2016 at 06:05 PM

From the Unity manual

Note for C# and Boo users: use Awake instead of the constructor for initialization, as the serialized state of the component is undefined at construction time. Awake is called once, just like the constructor.

http://docs.unity3d.com/ScriptReference/MonoBehaviour.Awake.html

So change Start to Awake.

Comment
Add comment · Show 3 · 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 lassade · Jan 26, 2016 at 06:08 PM 0
Share

Btw Start is called in the frame the game object is enabled.

avatar image incorrect · Jan 26, 2016 at 06:19 PM 0
Share

Start and Awake are both can be used, they are not constructors.

avatar image incorrect · Jan 26, 2016 at 06:21 PM 0
Share

Futhermore, both can be executed the same frame.

Like the Awake function, Start is called exactly once in the lifetime of the script. However, Awake is called when the script object is initialised, regardless of whether or not the script is enabled. Start may not be called on the same frame as Awake if the script is not enabled at initialisation time.

($$anonymous$$onoBehaviour.Start() description)

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

39 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

Related Questions

How to manage player and enemies shooting sounds at the same time? 1 Answer

Instanced Prefabs: Audio clip array out of range 1 Answer

Do unused audio sources hurt performance? 0 Answers

load audio files with Resources.Load and play them 7 Answers

Audio loops too early 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