- Home /
Button not instantiating, no errors
Hello,
I'm pretty new to the programming world, as such I am planning on running through the exact steps of my problem here. Note that I am using Unity 5.0.1, so if you think it is just a problem with the version I will upgrade (I'm just too lazy too, since there isnt much too of a difference).
First, I created a new scene. Then, I made a new button using the dropdown menu. I then changed its dimensions and such to my liking.
Next, I created an empty prefab, and then filled it with the button using drag and drop. To test if it was working, I deleted my button in the scene, then dragged and dropped the prefab on the scene, creating a new button.
Finally, I set out to instantiate the button using a script. This is the only line of code that I have related to this, all variables in it were properly set.
GameObject chunk = Instantiate(Resources.Load("Chunk"), chunkMap[x,y,z].position, Quaternion.identity) as GameObject;
The prefab was named "Chunk". From what I can tell, this should create an object in the scene, which can be seen in the hierarchy, and at the location I set it to. However, nothing shows up on screen or in the hierarchy.
After some research the only thing that could possibly be needed additionally is a line of code somewhere that looks like var Chunk: GameObject; but anywhere I put that it doesn't seem to work.
Any time I run it I get no errors, any debug line I stick in there indicates that the code does reach, and pass that line. The "Chunk" prefab is in a Resources folder.
What am I missing (or doing wrong)?
Answer by Aezora · Apr 04, 2017 at 11:06 PM
Well, I figured it out. Turns out it was spawning in the wrong scene.... Because I was calling it from the wrong scene...
Well this is awkward.
Answer by AndreM1 · Apr 04, 2017 at 02:51 PM
That's really weird, I can't EXACTLY help your code since 5.0.1 is kinda outdated, but I can tell you what works for me in at least in 5.3. So maybe you want to test it if works.
You should really consider upgrading unity, there are some really nice features, and some weird bug fixes :)...
*You don't need to do Resources.Load(); do get a prefab from the source, this will cause performance issues everytime this code runs, instead, do this:
using UnityEngine;
using System.Collections.Generic;
public class Script:MonoBehaviour{
public GameObject chunkPrefab;
GameObject ch = Instantiate<GameObject>(
chunkPrefab, chunkMap[x,y,z].position, Quaternion.identity);
}
or, you also might want to reference .localPosition, and remove the generic if you can.
GameObject ch = Instantiate(
chunkPrefab, chunkMap[x,y,z].localPosition, Quaternion.identity);
Well, I tried this and it didn't seem to do anything. Although, when I tried to put <GameObject>
next to the instantiate like you had, it gave me an unknown resolve error, so I ended up reverting to as GameObject
. I guess I should probably update then...