A little help needed [C#]

Well my problem is that I use massives, and it seems that building time is out of sync. My problem is that I make a public massive of AudioClip’s and I want to make an array of AudioSource’s in Start() method. But it’s not doing so and i think the reason is that it doesnt know the Length of the massive in Start() method. To explain better, here is my code:

using UnityEngine;
using System.Collections;

public class SoundController : MonoBehaviour {

	public AudioClip[] onSamples;
	
	private AudioSource[] onSamplesSource;
	
	void Start ()
	{		
		for (int i=0; i< onSamples.Length; i++)
			onSamplesSource[i] = CreateAudioSource(onSamples[i]);	
	}

	public AudioSource CreateAudioSource (AudioClip clip) {
		GameObject go = new GameObject("audio");
		go.transform.parent = transform;
		go.transform.localPosition = Vector3.zero;
		go.transform.localRotation = Quaternion.identity;
		go.AddComponent(typeof(AudioSource));
		go.audio.clip = clip;
		go.audio.loop = true;
		go.audio.volume = 0;
		go.audio.Play();
		return go.audio;
	}
}

Any sugestions? Thanks for your time!

Try this:

void Start()
{
	int len = onSamples.Length;
	onSamplesSource = new AudioSource[len];

	for (int i = 0; i < len; i++)
	{
		onSamplesSource[i] = CreateAudioSource(onSamples[i]);   
	}
}

Thanks man! this worked perfectly!

The problem you were having and jvil answer was that the array was not initialized to any capacity. The change suggested would create an array of the same size as onSamples(for onSamplesSource) and then fill the array with the loop.

A step further, no extra variables needed:

void Start()
{
	onSamplesSource = new AudioSource[onSamples.Length];

	for (int i = 0; i < onSamples.Length; i++)
	{
		onSamplesSource[i] = CreateAudioSource(onSamples[i]);   
	}
}

That’s a bad practice, you already know the array length, so you don’t need to ask for the length on every loop: onSamples.Length

Using a fixed variable it improves performance, specially on mobile. An extra variable with their final length is strongly recommended on each loop.

technically both are fine

the speed difference is extremely negligible.

The length of an array is not recalculated every loop of the for loop. Arrays are static size, the Length property only has the overhead of a property read.

Other enumerables may have a recalculation overhead, if they’re one that has variable length. But indexing that kind of enumerable is stupid and you should use a foreach on it.

But with an array. That overhead of a property read is so minor that you won’t even see a difference until the array got to like 10000000 in length. Which only will have a millisecond in difference. And when you’re at that size… you should me more concerned about memory overflow than that millisecond speed boost. And this goes for mobile devices as well…

Landern’s position is probably more influenced by standardized code practices. It’s common practice not to create new variables if one already exists. And properties are considered as existing variables for a value.

Prefer standards and readability over frivolous optimizations.

Still though, neither of you are wrong. It’s not like jvil’s code is unreadable or anything.

Nope. The C# compiler uses inlining to query the length of the array, so adds no processing overhead.
Secondly, compilers nowadays are smart, so this sort of check would be optimised internally.

Caching values (if possible) is always recommended, same for transforms and other objects. Maybe this isn’t the best example, but there are a lot of cases where you can see a biggest difference caching this value or not.

According to the DalvikVM designer Dan Bornstein (Google Engineer) these are the fastest ways of looping, sorted from fast to slow: http://www.androidsnippets.com/how-to-write-faster-loops

If you can make faster code (even in ms), why you code something slower?

I wasn’t saying you were wrong. And yes you save a millisecond on HUGE arrays (note, that is an unreasonably huge array).

My point was that what Landern said is NOT a “bad practice” as you said. Far from.

If you feel like saving that 1 millisecond. Go for it. I don’t agree with Landern correcting you on that, you don’t need to be corrected. But just because you prefer to do it, doesn’t make Landern wrong, and it’s common practice to NOT create extra variables when a variable or property already exists.

We can post different engineers saying different things about it. Because as I already stated… no one is wrong on the matter. It’s personal preference.

Oh and your link… that depends on the language and compiler. Dan Bornstein doesn’t specify which it was… As Meltdown pointed out, modern compilers smart compile code.