- Home /
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[i].ToString());
if (tracks[i].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[i]);
else
newSpeaker.SendMessage("Go", tracks[i]);
}
}
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?
Answer by Adam-Halley-Prinable · Oct 02, 2014 at 06:37 AM
I didn't. Line 23 is where i...AAH! ARGH! Deleted line 8, works now. I was accidentally removing my prefab! Thanks man.
@Bored$$anonymous$$ormon 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
Your answer
Follow this Question
Related Questions
How to Select between two prefabs randomly ? 1 Answer
Simple Instantiate gameObject problem 3 Answers