Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gdg · Jul 29, 2013 at 08:36 AM · gameobjectinstantiateprojectile

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?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gregzo · Jul 29, 2013 at 08:55 AM 0
Share

Where is arrowParticle delcared ? Is it of type Projectile ?

avatar image gdg · Jul 29, 2013 at 09:11 AM 0
Share

public Transform arrowParticle;

I drop a prefab into it

$$anonymous$$aking it a GameObject makes no difference.

avatar image nixcs2512 · Jul 29, 2013 at 09:20 AM 0
Share

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.

avatar image robhuhn · Jul 29, 2013 at 09:35 AM 1
Share

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>();

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by gregzo · Jul 29, 2013 at 09:24 AM

  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 );
 }


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gdg · Jul 29, 2013 at 10:05 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Skullwing · Jul 29, 2013 at 09:40 AM

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges