Instantiate does not return

I use this code :

private Projectile clone;

void Update()
{
clone = Instantiate(arrowParticle, arrowFirePos.position, transform.rotation) as Projectile;
}

The object is instantiated correctly but clone stays null I can’t figure out why?

  1. Instantiate the GameObject you are referencing ( prefab )
  2. Use GetComponent if you need to access or cache a reference to a component attached to that GameObject

Or directly reference your prefab as one of it’s components ( rigidbody example in the docs ).

Your problem here is that you are referencing your prefab as one type (Transform), and trying to reference it when instantiating as another (Projectile).

One correct example, keeping references to every instantiated projectile in a List ( requires using System.Collections.Generic ) :

public Projectile projectilePrefab;
private List < Projectile > instantiatedProjectiles = new List < Projectile > ();

void Update ()
{
   Projectile newProjectile = Instantiate(projectilePrefab, arrowFirePos.position, transform.rotation) as Projectile;
   instantiatedProjectiles.Add ( newProjectile );
}

is Projectile is GameObject or a prefab if its a prefab Make private to public gameObject and attache the prefab to the Projectile Object. Add the arrowParticle too for transform position.

Instantiate a Projectile with force

The same …

In both is a working instantiation for a projectile…it should help you.

  • What does the console print?