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 RobRab2000 · Jan 17, 2014 at 10:53 AM · c#variablesfunctions

Can I pass a variable to a function to be assigned in c#

I'm trying to build a single function that I can use to assign all of my audio clips from the resources folder so that my code looks neater and is not repeated over and over (I've been told this is good). I know its not a whole bunch of code lines that I'm saving but this is as much an excursive in learning how to code better as it is for practical use..

At the moment this comes us saying that it cannot assign a variable to null. I think this is because its not passing the empty variable slot (what is the right terminology for this btw? Pointer?) so it doesn't work?

(p.s. I know i haven't to it looping through my sfx yet. I actually built an array that holds them but i took it out so that I could stry to get this part working first.)

 using UnityEngine;
 using System.Collections;
 
 public class BaseWeapon : MonoBehaviour {
 
 protected AudioClip _fireSFX, _reloadSFX, _clipEmptySFX;
 
 void Awake () {
         LoadAudioClipFromResources (_fireSFX, (gameObject.name + "_fireSFX"));
     }
 
 public virtual void LoadAudioClipFromResources(AudioClip clipSlot, string clipName){
         print ("Assigning Clip: " + clipName + " to Slot " + clipSlot);
         if (clipSlot == null) {
             if (Resources.Load (("_Audio/_SFX/Weapons/" + clipName), typeof(AudioClip)) == null) {
                 print ("Cannot find AudioClip for " + gameObject.name);
             }
             clipSlot = Resources.Load (("_Audio/_SFX/Weapons/" + clipName), typeof(AudioClip)) as AudioClip;
         }
     }
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

Answer by JNSVS · Jan 17, 2014 at 11:43 AM

In the above given code snippet , you are trying to load a audio resource into a parameter passed into the function.

void FunctionName(parameter1,parameter2,....);

in c# or any language, parameters passed to the function - when a function is called with parameters. A copy of the passed parameter is used. which gets destroyed after the function call gets over. You are not accessing the actual variable (in ur case _fireSFX) but a copy of _fireSFX(which is empty when the function was called). And the function loads the audio clip into the copy which is created for the function, as soon as the function call gets over, this variable is cleared.

Basically _fireSFX was passed as empty variable to the function and remains empty even after ur function call ( coz you loaded data into a copy of the varible).

What you are trying to do is achieved using pointers in C++ (where you make changes to the actual variable stored in the addressed passed to the function). In C# you can achieve the same by passing an array otherwise pointers are not present in c# or java.

You can achieve what you are doing by 2 ways:

  • Method one (return type)

Code Snippet:

     {
         protected AudioClip _fireSFX, _reloadSFX, _clipEmptySFX;
     
     void Awake () {
     
         //Assigning the returned Clip into our Target clip
         _fireSFX = LoadAudioClipFromResources (_fireSFX, (gameObject.name + "_fireSFX"));
     }
     
     public virtual AudioClip LoadAudioClipFromResources(AudioClip clipSlot, string clipName){
         print ("Assigning Clip: " + clipName + " to Slot " + clipSlot);
         if (clipSlot == null) {
             if (Resources.Load (("_Audio/_SFX/Weapons/" + clipName), typeof(AudioClip)) == null) {
                 print ("Cannot find AudioClip for " + gameObject.name);
             }
             clipSlot = Resources.Load (("_Audio/_SFX/Weapons/" + clipName), typeof(AudioClip)) as AudioClip;
         }
             //Sends the loaded clip back
             return clipSlot;
         }
     }

 

  • Pass by reference (indirect pointers)

Code Snippet

 protected AudioClip _fireSFX, _reloadSFX, _clipEmptySFX;
     AudioClip[] _ListofClips;
 
 void Awake () {
 
     //Assigning the returned Clip into our Target clip
     LoadAudioClipFromResources (_ListofClips, (gameObject.name + "_fireSFX"));
 }
 
 public virtual void LoadAudioClipFromResources(AudioClip[] clipSlot, string clipName){
     print ("Assigning Clip: " + clipName + " to Slot " + clipSlot);
     if (clipSlot == null) {
         if (Resources.Load (("_Audio/_SFX/Weapons/" + clipName), typeof(AudioClip)) == null) {
             print ("Cannot find AudioClip for " + gameObject.name);
         }
             for(int i=0;i<clipSlot.Length;i++){
             clipSlot[i] = Resources.Load (("_Audio/_SFX/Weapons/" + clipName), typeof(AudioClip)) as AudioClip;
             }
     }
 
     }
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
avatar image
0

Answer by RobRab2000 · Jan 17, 2014 at 03:42 PM

Worked like a charm :)

I used the first method because it meant I didn't have to write code loading all the SFX and audio clip slots into arrays. Also it means that i can swap between generic sfx files and ones that include the game object name without having to modify or duplicate the function!

Awesome, thanks for the help :)

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 JNSVS · Jan 19, 2014 at 01:02 PM 0
Share

You are welcome! Please vote & mark as answer so that it helps others as well.

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

19 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

Related Questions

Referncing variables such as OnTriggerEnter(Collider other) thumb rule? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Variable declaration. Public vs indirect public? 3 Answers

Random selection 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