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 LifeArtist · Apr 28, 2014 at 05:10 PM · c#instantiate

Instantiate and save it to a variable

Hey,

I have a problem with instantiating my object as rigidbody into my clone variable :(

I tried also ... = (Rigidbody2D)Instantiate(...)

Nothing happens. Every time he is printing Null ( so the var is empty ).

 using UnityEngine;
 using System.Collections;
 
 public class playerSpawn : MonoBehaviour 
 {
     public FollowingCamera followingCamera;
     public GameObject playerPrefab;
     public Transform spawnPoint01;
     public Rigidbody2D clone;
 
     // Use this for initialization
     void Start () 
     {
         clone = Instantiate(playerPrefab, spawnPoint01.position, spawnPoint01.rotation) as Rigidbody2D;
         print(clone);
         sendToCam(clone.transform);
     }
 
     void sendToCam(Transform playerName)
     {
         followingCamera.player = playerName;
     }
 }

Regards,

LifeArtist

Comment
Add comment
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

1 Reply

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

Answer by wibble82 · Apr 28, 2014 at 05:21 PM

Hey there

First up, a lesson on casting as its very important. I see a lot of people using the 'as' keyword. As you probably know, objects in c# have a specific type and if a function such as 'Instantiate' returns a plain 'Object', you can cast it to the type you believe it to be. However there are 2 key forms cast:

  • the static cast: clone = (Rigidbody2D)Instantiate(bla) will throw an exception (i.e. report an error in the console) if Instantiate doesn't return an object of type RigidBody2D. Use this when you know you are expecting a specific type, and it is an error if you don't get one.

  • the dynamic cast: clone = Instantiate(bla) as Rigidbody2D will just silently return null if Instantiate doesn't return an object of type RigidBody2D. It's useful when you want to test what type of object is actually returned (see also the 'is' keyword) and take action based on the result.

Incidentally, the dynamic cast is also slower, so if you're building for mobile its worth avoiding where possible.

In general though, if you are specifically expecting an object of a given type then the static cast is the one to use because it'll actually report an error if something unexpected happens.

Now in your case the playerPrefab is a GameObject, so Instantiate is going to create and return a GameObject. You've done that right - this is how Instantiate should be used.

However, you are then using a dynamic cast to try and convert it to a Rigidbody2D. Because Rigidbody2D is not derived from GameObject, the dynamic cast fails and returns null. Had you used a static cast you would have got an exception instead.

I suspect that you want to make 'clone' a GameObject as well. You can still access its rigidbody2d component using the builtin rigidbody2d property. i.e.: clone.rigidbody2d

Hope that helps.

-Chris

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 LifeArtist · Apr 28, 2014 at 05:50 PM 0
Share

Very nice explanation !

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

22 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 avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Creating an Object with a string C#! How? 1 Answer

Instantiate does not exist in current context. 1 Answer

Emit particles on CollisionEnter2D works on one collision, not the other? 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