- Home /
Creating a GameObject variable without instantiating it?
I'm currently working on first person shooter mechanics, but I'm trying to make it as portable as possible (ideally with the ability to drag and drop the script onto an empty GameObject and have it work. I've been doing this by checking if a component is null, e.g. rigidbody. If it's null then I create the component and set some default attributes to it. This all works until I try to make the bullet that will be fired.
Essentially what I'm trying to do is create a prefab within a script that I can instantiate at a later point in time. This "projectile" object will just be a basic sphere. I have tried using GameObject.CreatePrimitive(), but this generates it. How would I go about creating a GameObject from inside a script without instantiating it?
This is what I currently have but it instantiates a projectile.if(projectile == null) { GameObject.CreatePrimitive(PrimitiveType.Sphere); transform.localScale = new Vector3(0.1f, 0.1f, 0.1f); }
Answer by RobAnthem · Dec 12, 2016 at 12:50 AM
GameObject myBullet = AssetDatabase.LoadAssetAtPath("Assets/myFolder/myBullet.prefab", typeof(GameObject)) as GameObject;
should work, from my experiences, it stores the object into the ram, and you can instantiate as many copies of the loaded object as you want.
if (!File.Exists("Assets/myFolder/myBullet.prefab"))
{
GameObject tempBullet = GameObject.CreatePrimitive(PrimitiveType.Sphere)
AssetDatabase.CreateAsset(tempBullet, "Assets/myFolder/myBullet.prefab");
Destroy(tempBullet)
}
myBullet = AssetDatabase.LoadAssetAtPath("Assets/myFolder/myBullet.prefab", typeof(GameObject)) as GameObject;
Your answer
Follow this Question
Related Questions
How can I spawn gameObjects within a circle radius? 1 Answer
How can I add the OnTriggerEnter function to all game objects that I instantiate? 1 Answer
Refresh panel with prefab contained value from json that created using array 0 Answers
How do i Instantiate gameObjects in between multiple points? 2 Answers
How do i Instantiate Objects to a Empty GameObject as a Child?? 3 Answers