- Home /
Instantiated Prefab's script doesn't work
Didn't anyone have this problem? Please help i moved to udk just because of this problem. But udk is really hard to understand for me.
I have a prefab and a script attached to it. When i instantiate prefab in runtime with code the script doesn't work properly. But when i just put the prefab in the scene without code everything works fine.
Below script is attached to a cube that is where i want the prefab's instantiate position
public static var notarray: Array;
var timearray: Array;
public static var i=0;
//i drag the prefab here in inspector view
var ob: Transform;
function Start () {
notarray=new Array();
timearray=new Array();
notarray.Add(ob);
timearray.Add(0);
notarray.Add(ob);
timearray.Add(1);
notarray.Add(ob);
timearray.Add(1);
notarray.Add(ob);
timearray.Add(1);
while(notarray.length>i){
yield WaitForSeconds(timearray[i]);
Instantiate(notarray[i],transform.position,transform.rotation);
i++;
}
}
/// This question is edited because i understood there wont be any answers, maybe this time?
hvilela--- i control collision in script with on trigger enter then in update function i control what will happen when in trigger, everything works fine if i just drug the prefab to the scene, but if i instantiate it in game collision codes doesnt work properly-- with properly i mean this: This is a guitar hero game, notes come and you press the key, when a long note comes it is supposed to don't disappear untill the user don't do it wrong but it disappear if i instantiate prefab in runtime. another example: some other prefab even doesn't do anything, nothing works with the script.
The only reason that the script doesnt work is that i instantiate it in runtime. Why i want to do this in runtime? Because i need to put the notes in specific positions as you can see above i have a yield Wait for Second method that waits for the time that is in the timearray.
$$anonymous$$iraSensei: Does it matter? I made it because maybe i want to reach it from other scripts.
Answer by RashidBekar · Oct 06, 2012 at 08:13 PM
I solved the problem. I can't tell how because i did a lot of things. Maybe later, i can explain it in here. So the others have this problem can solve it too..
Answer by haico1992 · Oct 06, 2012 at 05:03 PM
Maybe you just need to put those code in to a new function other than Update() and Start(), since it have "yield";
Here what I do: Make a script with a public variable :Transform;
Attach that script to any object in scene, Main camera for example.
Drag the prefab you want to instantiate to the variable.
Instantiate those prefab in that script.
function YourCode()
{
Doing Stuff
}
function Start(){
YourCode();
}
Your answer
Follow this Question
Related Questions
Prevent instantiating on collider 1 Answer
Scripts of instantiated objects 1 Answer
Trigger between 2 kinematic bodies not firing? -1 Answers
Instantiate A Prefab 1 Answer
Instantiated Prefab Trigger not firing. 0 Answers