- Home /
Instantiate not spawning prefab
I have a script that spawns a player when you press the play button from a different script. Everything is working fine, but the thing is the prefab isn't showing up. Heres the code:
var playerGM : GameObject;
var startFlagPos : Vector3;
var startFlag : GameObject;
function Update () {
if (WorldEditWindow.test == true) {
startFlag = GameObject.Find("Start Flag");
if (!startFlag) {
Debug.LogWarning("There is no Start Flag in the level!");
} else {
Debug.Log("Spawning Player...");
startFlagPos = startFlag.transform.position;
Instantiate(playerGM, startFlagPos, Quaternion.Euler(0,0,0));
GameObject.Find("Main Camera").SetActive(false);
GameObject.Find("VisualizerCube").SetActive(false);
GameObject.Find("BuildGrid").SetActive(false);
GameObject.Find("BuildGridBottom").SetActive(false);
}
}
}
I tested everything, and everything is working, the start flag is being found and such, but Instantiate is just not working in general.
Can you see the object in the Hierarchy? That is, is it just not showing up in Game view, or is it not getting created at all?
The object is not being shown in the Hierarchy and the Game view. It looks like its not being created at all.
I don't know what WorldEditWindow.test is. But you would be better of declaring the startFlag in Start()
GameObject.Find() in Update() can cause bugs, may be the cause of problem.
Also Instantiating is a pretty performance heavy operation if that is being called every Update() frame.
Also, use Quaternion.identity
ins$$anonymous$$d of Quaternion.Euler(0,0,0)
. You'll save many multiplications and a few trig calculations as it calculates to quaternion from the Euler angles.
WorldEditWindow.test is a static variable from script WorldEditWindow, which has a gui button that triggers WorldEditWindow.test boolean variable which then triggers all of the code above.
Answer by zardini123 · Oct 07, 2013 at 08:12 PM
Ok i got it to work! What I did is that when the Start Flag is spawned, I disabled that script that I said destroys the blocks thats in the same location. So now it works perfectly!
Its funny how people (thats me) panic but then it was a very dumb mistake! :)
Thanks all!
It happens to everyone. This kind of error (false positive for bad state checks) crops up quite a lot, and it can be particularly nasty if you don't know to look for it.
Answer by $$anonymous$$ · Oct 06, 2013 at 10:42 PM
Create a resources folder and asset's and pick apart the code I've used in another game:
If you miss the (Clone) bit instantiate adds, it can lead a person astray.
void OnLevelWasLoaded() { GetComponent ();
if (OnSelectGui.maleDemonKnight == true){
GameObject newPlayer = (GameObject)Instantiate(Resources.Load("DarkKnight"));
}
if (OnSelectGui.femaleDemonKnight == true){
GameObject newPlayer = (GameObject)Instantiate(Resources.Load("DemonFem"));
}
void Start () {
var spawn = GameObject.Find("spawn");
var DarkKni = GameObject.Find("DarkKnight(Clone)");
var DarkFem = GameObject.Find("DemonFem(Clone)");
if (OnSelectGui.maleDemonKnight == true){
DarkKni.transform.position = spawn.transform.position;
}
if (OnSelectGui.femaleDemonKnight == true){
DarkFem.transform.position = spawn.transform.position;
}
}
Look, the thing is that the gameObject im looking for, which is the Start Flag, is not in the scene when you load the scene. The user places the start flag, then when you press a button, itll spawn a player at the start flag.
Neither is $$anonymous$$e :), what this script does is instantiate based on a decision made in a different scene. So there's no tie together whatsoever and the character doesn't exist in the new scene.
So what it does then is look in my resources folder from void OnLevelWasLoaded()and dependent on what character is set to true, it then instantiates it to an empty gameobject placed in the scene I called spawn.
Update is way too late to be instantiating:
startFlag = GameObject.Find("Start Flag");
You have to call from resources, or where does it get Start Flag from?
Next it won't be called Start Flag, when you instantiate something Unity automatically adds Start Flag(Clone) on the end.
Ok, to get all of the shinanigans out of the way, Im gonna give you the sequence of what happens.
1) Scene Loads. Scene is empty.
2) Player creates scene by using the selected game objects. When the gameObjects are Instantiate, the script automatically removes the (Clone) at the end of the name with the code of:
var thingSpawned : GameObject = Instantiate(Instantiate stuff goes here);
thingSpawned.name.Replace("(Clone)", "");
3) When Player is ready, they press the Test button, which calls the script TestWorld which looks for the Start Flag in the scene that has been placed by the Player and spawns a PlayerGameObject at that location.
Also the playerG$$anonymous$$ is assigned through the Inspector in Unity.
That's your issue - that bit of code. The method string.Replace
doesn't do what you think it does. Strings are immutable (that is, once created, they can't be changed), so the only way to alter a string is to re-assign it. Replace doesn't change the existing string, it returns a new string that contains the new value, while the existing string remains the same - that code won't change thingSpawned.name
. Try doing this:
thingSpawned.name = thingSpawned.name.Replace("(Clone)", "");
Sorry i meant what Hoeloe typed. But seriously guys, the stuff you are telling me is wrong, like the name of the gameobject, isnt the problem. Thats NEVER been the problem! I've already done hours of debugging and crap and the Object in the Herhechiey (man i cant spell) is "Start Flag" not "Start Flag(Clone)", so please just stop about the name of the Object! Thats not the problem. Its the Instantiate thats causing the problems. All of the variables most likely are not even close to the problem. I used the Debug.Log method to track the result of GameObject.Find and it DOES find the Start Flag.
By the way, the Debug.Log("Spawning Player...") does initiate and does show in the log. So that concludes that the method is being called and the if statement is working. Its just the stupid Instantiate.
Your answer
Follow this Question
Related Questions
Game Works on Computer, but not on iPhone 0 Answers
Main Menu scrpit not working 1 Answer
Unity 3d android not working 1 Answer
Ocean not rendering (only during runtime) 1 Answer
Mono - Develop not working? 1 Answer