Hello, thanks for all the help so far. People have overall been very nice to assist with the harsh learning curve in Unity
I have run into a very simple problem i guess, i have been googling a bit but can’t seem to find the answer.
let’s say i reference a prefab I’ve made, don’t know if this is the correct way of describing it. But, i reference Spell23Prefab by using:
public GameObject Spell23Prefab;
then i save, and drag the prefab into the slot that appears in unity. This is all fine. But if i try doing the same with an empty gameobject, already in the scene, i can’t really drop it in a similar slot. Do i need to use some other wording? public gameobject doesn’t work. So how do i refer to an empty gameobject? Like how do i tell the script which empty gameobject i want it to focus on
Hi,
It should work with the dragging but if not, use:GameObject Spell123Prefab = GameObject.Find(“ name of the object”)
If you have an empty GameObject in your scene, you can still drag and drop it into a variable that is public GameObject.
If that doesn’t work, you have a different issue on hand that is preventing it from working.
The GameObject you want to reference still needs an identifier, i.e. you need to give it a “name” just like you called the other “Spell23Prefab”.
public GameObject myEmptyGameObject;
It is called SpellSpawn in the scene, and i used: “public GameObject SpellSpawn;” in the script. It wouldn’t let me drop it there, from the scenes hierarchy.
Yeah thanks, but since i need to reference 81 gameobjects, i guess it would be better to do it in the editor, right? Probably minimal difference when it comes to performance though.
Make sure you don’t have any compiler errors.
Well, it was because i was trying to reference a gameobject, in the script of a prefab in my assets folder. As soon as i dragged it into the scene, i could do it just fine.
Not sure what you actually said, but yes, you can’t add stuff to a prefab from your scene.
1 Like
I was having a similar problem with the editor, and the solution was that the reference to the empty was a prefab, and making the empty into a prefab fixed it.
This is a bit advanced, but for 81 objects you wouldn’t hand-make them. You’d have the game create them on start-up, storing the links as it goes. Ex:
public GameObject emptyPF;
GameObject[] Spells;
public int spellCount = 81;
void Start() {
Spells = new GameObject[81];
for(int i=0;i<spellCount;i++) {
Spells[i] = Instantiate(emptyPF);
Spells[i].name = "spell"+(i+1); // for example
}
If they were so unique that you must premake them, you could put them inside another empty “spell holder”. Organize them in the order you want. Now spell #n is: spellHolder.getChild(n);
.
1 Like