instantiating an imported prefab

I dragged a prefab from a folder outside of my project into my assets in Unity. The name of the prefab is “Explosion”.

I tired to instantiate it three different ways based on reading documentation and examples found online:

GameObject e = Instantiate (Explosion) as GameObject;
GameObject e = (GameObject) Instantiate("Explosion");
Instantiate(Explosion,new Vector3(3,10,0),Quaternion.identity);

I get these errors:
error CS0103: The name Explosion' does not exist in the current context error CS1502: The best overloaded method match for UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)’ has some invalid arguments
error CS1503: Argument #1' cannot convert object’ expression to type `UnityEngine.Object’

Can anybody tell me where I’m going wrong?

Please show your complete script.

code is attached to main camera.

using UnityEngine;
using System.Collections;

public class cameraScript : MonoBehaviour
{

   public void genPart()
   {
     //generate random 3d location
     float partXloc = Random.Range (-3f, 3f);
     float partYloc = Random.Range (-2f, 3f);
     float partZloc = Random.Range (6f, 12f);

     //test instantiate particle
     GameObject m_CurrentParticle = (GameObject) Instantiate("Explosion");
     //GameObject e = Instantiate (Explosion) as GameObject;
     //GameObject e = (GameObject) Instantiate("Explosion");
     //Instantiate(Explosion,new Vector3(3,10,0),Quaternion.identity);
     //GameObject e = new GameObject("Explosion";

     //move particle to 3d location
     e.transform.position = new Vector3(partXloc, partYloc, partZloc);
   
     //set particle to self destruct
     Destroy(e.gameObject, 4);
   }


   void Start()
   {
     genPart ();
     InvokeRepeating("genPart", 4,4);
   }

}

http://unity3d.com/learn/tutorials/modules/beginner/scripting/instantiate

Thanks, Dantus.

I see the prefab name is “Rocket”, but only “prefabRocket” is used in the script. How does that work?

The prefab’s name doesn’t matter. The UsingInstantiate script is attached to a game object in the scene. If this game object is selected, there is a slot named rocketPrefab in the UsingInstantiate component. The Rocket prefab needs to be dragged into that slot. Now when you call Instantiate (rockPrefab), it will pick the prefab that was assigned to that particular slot.

1 Like

What if I wanted to instantiate a prefab particle system from a collision script placed on an instantiated bullet? Such as:

using UnityEngine;
using System.Collections;

public class targetColScript : MonoBehaviour {

   void OnCollisionEnter(Collision col)
   {
     if(col.gameObject.name == "projectile")
     {
       flingScript.points += 10;
     }
   }
}

This code changes the points variable nicely, but how would I add code to instantiate an explosion on the collision, too?

The idea how you can instantiate prefabs in Unity doesn’t change in that context. Try to follow the tutorial to understand what is going on. If you have trouble to understand it at a certain point, don’t hesitate to ask.

1 Like

I guess my problem is that I’m an old school programmer who’s used to creating only one long piece of code that controls everything. Placing different scripts on different things for different reasons is confusing to me.

so the UsingInstantiate script is placed on the rocket? The video doesn’t actually show any of the scripts being placed anywhere except for the Destroy script. I guess we’re just supposed to be psychic and know where they go, lol? I’m so confused.

I didn’t include my whole code because honestly I thought this would just be a syntax problem, but I guess it’s more complicated than that, so here is all my code:

I have a script called fireScript that I have placed on the camera:

using UnityEngine;
using System.Collections;

public class fireScript : MonoBehaviour
{

   //PUBLIC VARS;
   public float targetFixedTime = 4f;
   public static int points = 0;


   //FIRE SPHERE FUNCTION
   public void fireSphere(float xVect, float yVect, float zVect)
   {
     //create sphere
     GameObject sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
     //name sphere
     sphere.gameObject.name = "projectile";
     //scale sphere
     sphere.transform.localScale = new Vector3 (0.1F, 0.1F, 0.1F);
     //add a rigidbody
     Rigidbody spheresRigidBody = sphere.AddComponent<Rigidbody> ();
     //add physical properties
     spheresRigidBody.mass = 1;
     spheresRigidBody.drag = 0;
     //move sphere to camera location;
     sphere.transform.position = Camera.main.transform.position;
     //fire sphere
     sphere.rigidbody.AddForce (new Vector3 (xVect, yVect, zVect));
     //destroy sphere after 5 seconds
     Destroy(sphere.gameObject, 5);
   }


   public void genTarget()
   {
     float tarXloc = Random.Range (-3f, 3f);
     float tarYloc = Random.Range (-2f, 3f);
     float tarZloc = Random.Range (6f, 12f);

     //create target
     GameObject target = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
 
     //add physical properties
     Rigidbody targetRigidBody = target.AddComponent<Rigidbody> ();
     targetRigidBody.isKinematic = true;
 
     //add targetColScript to target
     target.gameObject.AddComponent<targetColScript> ();

     //scale target
     target.transform.localScale = new Vector3(1, 0.1F, 1);
     //rotate target
     target.transform.Rotate(90, 0, 0);
     //move sphere to random location;
     target.transform.position = new Vector3(tarXloc, tarYloc, tarZloc);
 
     //set target to self destruct
     Destroy(target.gameObject, targetFixedTime - 0.2f);
   }


   void Start()
   {
     genTarget ();
     InvokeRepeating("genTarget", targetFixedTime, targetFixedTime);
   }


   void Update()
   {
       if(Input.GetButtonDown("Fire1"))
       {
         fireSphere(0f,100f,5000f);
     }
   }

}

Then I have a collision script that isn’t actually placed on anything, but is added to the instantiated target on line 49 of fireScript:

using UnityEngine;
using System.Collections;

public class targetColScript : MonoBehaviour {

   void OnCollisionEnter(Collision col)
   {
     if(col.gameObject.name == "projectile")
     {
       flingScript.points += 10;
     }
   }
}

Could you show me how and where you would add the code to instantiate the Explosion particle system upon collision of target and a sphere?

Thanks for trying to help, Dantus. I was reading that page a bunch of times before I came here to ask this question.

I also read this page:

this page:

this page:

this page:

and this page:

I also watched that video you linked to, as well as this one:

I even downloaded an IRC client and asked about this in #unity3d chat. I’ll keep asking there but so far I haven’t gotten much help.

I wouldn’t just come here without trying to figure it out myself as hard as I could, or come here with the attitude that everybody should do my work for me. For some reason I just can’t figure this thing out. I don’t know what I’m missing. Maybe I’m just stupid. It would be really really really really really awesome if someone could just type out the code I need to use to do this.

In each of the approaches, they have an instance variable in the class like:

...
public class ... {
...
    public GameObject explosionPrefab;
...
}

This is the actual prefab which is going to be instantiated. In order to instantiate it, you will have to call:

Instantiate (explosionPrefab);

As you add this script to at least one game object, you need to make sure that you drag your Explosion prefab into the slot named explosionPrefab.

1 Like

Thanks, Dantus!

I added both pieces of that code to my targetColScript. So now in full it reads:

using UnityEngine;
using System.Collections;

public class targetColScript : MonoBehaviour {

   public GameObject explosionPrefab;

   public void explodeIt()
   {
     Instantiate (explosionPrefab);
   }

   void Start()
   {
     //try to instantiate particles every 2 seconds just to see if the code works
     InvokeRepeating("explodeIt", 0, 2);
   }

   void OnCollisionEnter(Collision col)
   {
     if(col.gameObject.name == "projectile")
     {
       flingScript.points += 10;
       Instantiate (explosionPrefab);
     }
   }
}

Then I dragged the Explosion prefab to the explosionPrefab slot in the inspector like you said.

When I play the game though, I get the error: “ArgumentException: The thing you want to instantiate is null.”

However, I tried creating just the default particle system, and then dragging that to the prefabExplosion slot, and it works fine! I guess the problem is with the Explosion particle system I imported from the asset store.

Thanks again for helping me with this, I’m sure I’m becoming a pain in your butt.:frowning:

Good to hear it is now working! Everyone is used to those moments when something just doesn’t work because of a silly mistake. Don’t worry, it wasn’t painful.

1 Like