Why can't wont Instantiate() work here?

I’m creating an audio system for a game made in javascript by someone else. I use C# myself so i might just be having syntax trouble or something.
The code i’m making here takes a request for a sound to be played, creates an invisible speaker at the requesters position, makes the speaker a child of the requester, the speaker then plays the sound, and deletes itself.
Here is the code:

#pragma strict

public var tracks : AudioClip[];

public var speakerTemplate:GameObject;

function Start(){
	speakerTemplate = new GameObject();
}

public function Play(requestedTrack:SoundRequest){

	var found:boolean = false;

	for (var i = 0; i<tracks.Length; ++i)
	{
		print (tracks*.ToString());*

_ if (tracks*.ToString() == requestedTrack.trackName.ToString() + " (UnityEngine.AudioClip)".ToString())_
_
{_
_
found = true;*_

* var newSpeaker = Instantiate(speakerTemplate, requestedTrack.client.transform.position, Quaternion.identity);*

* //make child of sender*
* newSpeaker.transform.parent = requestedTrack.client.transform;*

* if (requestedTrack.loop)*
_ newSpeaker.SendMessage(“GoLoop”, tracks*);
else*
newSpeaker.SendMessage(“Go”, tracks*);
}
}*_

* if (!found)*
* print (“ERROR: SOUND FILE NOT FOUND”);*
}
The problem is when I say “newSpeaker.SendMessage”, it always says SendMessage Go has no reciever. I pause the runtime and search for my speaker prefab and it’s not there, so for some reason my object isnt instantiating.
Why isn’t instantiate working?

2 Answers

2

You can’t use new to create a GameObject. Create a prefab and Instantiate that instead.

I didn’t. Line 23 is where i…AAH! ARGH! Deleted line 8, works now. I was accidentally removing my prefab! Thanks man.

@BoredMormon Yes you can. Using new to create a GameObject is perfectly valid. It creates a new empty GameObject in the scene. http://docs.unity3d.com/ScriptReference/GameObject-ctor.html