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;
		}
	}

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 *= Resources.Load (("_Audio/_SFX/Weapons/" + clipName), typeof(AudioClip)) as AudioClip;*
  •  	}*
    
  • }*

  • }*

Worked like a charm :slight_smile:

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 :slight_smile: