- Home /
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?
Where is arrowParticle delcared ? Is it of type Projectile ?
public Transform arrowParticle;
I drop a prefab into it
$$anonymous$$aking it a GameObject makes no difference.
Instantiate always return Object types so Projectile type clone cant be assigned to Object types. Try:
private GameObject clone;
void Update()
{
clone = (GameObject)(Instantiate(arrowParticle,arrowFirePos.position,transform.rotation));
}
A bit lack information about Projectile class so i dont know how to change clone to Projectile type.
If Projectile is a component on arrowParticle you cannot Instantiate and cast it the same time. The way you cast the object just returns null if casting fails:
clone = Instantiate(arrowParticle, arrowFirePos.position, transform.rotation) as Projectile;
Using this will throw an exception if casting fails:
clone = (Projectile)Instantiate(arrowParticle, arrowFirePos.position, transform.rotation);
So I would suggest to use:
GameObject clone = (GameObject)Instantiate(arrowParticle, arrowFirePos.position, transform.rotation);
cloneProjectile = clone.GetComponent<Projectile>();
Answer by gregzo · Jul 29, 2013 at 09:24 AM
Instantiate the GameObject you are referencing ( prefab )
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 );
}
thank you! I destroy them quickly and I want only one at a time (im using trails on them and this is for a mobile game) so I just made arrowPartcile a Projectile.
Answer by koushik.s · Jul 29, 2013 at 09:26 AM
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.
Answer by Skullwing · Jul 29, 2013 at 09:40 AM
Instantiate a Projectile with force
In both is a working instantiation for a projectile...it should help you. + What does the console print?
Your answer
Follow this Question
Related Questions
Creating more objects with code. 2 Answers
Trash Collection For Instantiated Game Object 1 Answer
Get components of multiple instantiated objects? 2 Answers
Can I use AddComponent to add (this.script) to an object? 1 Answer
Object Instantiate 1 Answer