Dear Unity,
I was looking for a way to make reference from Prefab to variable of type GameObject.
Inside the editor, it is no problem at all. Just drag and drop a prefab on to public variable, of type GameObject, inside your GameObject`s script.
But, queastion is, how to perform this “drag and drop” via script?
Example:
We know name of prefab lets say “Soldier”.
We have script like this. It will instantiate GameObject from variable ObjectTest.
using UnityEngine;
using System.Collections;
public class TEST : MonoBehaviour
{
public GameObject ObjectTest;
void Start ()
{
Instantiate(ObjectTest);
}
}
This will work only if “Soldier” prefab is referenced to variable in editor by drug and drop it. just like in this official Unity tutorial http://www.youtube.com/watch?v=4rZAAHevq1s&list=PLX2vGYjWbI0RmYmP19OimzWSy2PpLxMk7&index=23
While I do understand, I can create an empty GameObject with reference to Prefab, and then use it each time instantiate needed. But still… I do want to know how to do it by script.
Thank you for reading and hopefully answering. Have a nice day!