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
1
Question by Setzer22 · Jun 18, 2012 at 08:33 AM · gameobjectobjectconvert

Cannot convert object to gameObject

Hello everyone,

I'm getting an error with an scrpit. In the script I've got

 public GameObject randomObject;
 randomObject = Instantiate(randomModel, transform.position, transform.rotation);

So, Unity gives me the following error: Cannot implicitly convert type 'UnityEngine.Object' to UnityEngine.gameObject'. An explicit conversion exists (are you missing a cast?).

First of all, I didn't even know that there was a difference between object and gameObject in Unity, I'd like to know the difference. Also, in order for my script to work, I need to know which type of variable does the Instantiate function return (I assume it's an UnityEngine.Object variable), then, how can I access the transform for that variable (which is not a gameObject). I've found none of this in the documentation :S

Thank you very much

Comment
Add comment · Show 2
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 Mizuho · Jun 18, 2012 at 08:36 AM 2
Share
 randomObject = (GameObject)Instantiate(random$$anonymous$$odel, transform.position, transform.rotation);

OR

 randomObject = Instantiate(random$$anonymous$$odel, transform.position, transform.rotation) as GameObject;

You just have to cast the Object to be a GameObject (the code doesn't know that Instantiate gives you a GameObject unless you tell it). Instantiate returns an Object because it can be used to instantiate things other than a GameObject. When you use it for a GameObject though, you must tell the compiler "The return value from this function is a GameObject."

Note that once you cast the return Object from Instantiate, you will have a GameObject and can access the Transform component as usual.

 randomObject.transform
avatar image Setzer22 · Jun 18, 2012 at 08:40 AM 0
Share

Ok thank you very much ^^ That helped a lot.

If you want, you can post this as an answer so I can mark it as solved.

3 Replies

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

Answer by Setzer22 · Jun 18, 2012 at 01:11 PM

Ok, as anybody posted an answer, but my question was completely answered at the comments, i'll copy the answer that I was given and worked for me so I can mark this question as answered.

Mizhuo's comment:

  randomObject = (GameObject)Instantiate(randomModel,transform.position,transform.rotation);

OR

 `randomObject = Instantiate(randomModel,transform.position,transform.rotation) as GameObject;`

You just have to cast the Object to be a GameObject (the code doesn't know that Instantiate gives you a GameObject unless you tell it). Instantiate returns an Object because it can be used to instantiate things other than a GameObject. When you use it for a GameObject though, you must tell the compiler "The return value from this function is a GameObject."

Note that once you cast the return Object from Instantiate, you will have a GameObject and can access the Transform component as usual.

  randomObject.transform

PD: @Mizhuo, if you want to post your original answer as an answer so I can vote it I'll delete this one, I don't care about this karma system but as this is not my answer, I'm only posting it so other users can see this is actually answered.

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
2

Answer by debthikana · Feb 27, 2014 at 02:24 PM

This is not working . public GameObject ball; ball = (GameObject)Instantiate(cubeEffect, transform.position, transform.rotation); Unity gives me the error "can't convert from source type to destination type". Plz help

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 Cereal_Killa · Aug 22, 2015 at 03:08 AM 0
Share

I get the same error. InvalidCastException: Cannot cast from source type to destination type.

avatar image
1

Answer by Kurius · Jan 18, 2016 at 01:08 AM

You must feed the Instantiate method with a GameObject, if you want it to return a GameObject. The mistake many people make is to feed Instantiate with a Transform, and then expect Instantiate to return a GameObject. This is the wrong approach. For example @debthikana is feeding Instantiate a Transform called "cubeEffect". Instead it should be a GameObject called "cubeEffect" that is fed into Instantiate. Then Instantiate will successfully return a GameObject back to you.

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

6 People are following this question.

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

Related Questions

How to make a gameobject into a stream? 1 Answer

Whats the difference between an Object and A GameObject? 2 Answers

inspector,Component 0 Answers

Destroy object when transform.position.x > 10. 1 Answer

How to remove the GameObject having the Object 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