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 RanRanRu · Dec 03, 2011 at 05:35 PM · c#instantiateinheritanceinvalidcastexceptionextend

C# Extending Script causes InvalidCastException on Instantiate

This line works in the original script, but bugs(InvalidCastException) when run on the extended script.

 Transform obj = (Transform) Instantiate(projectile,loc,transform.rotation);


Any ideas? Thanks in advance.

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 RanRanRu · Dec 03, 2011 at 06:05 PM 0
Share

Digged around for a bit, restarting Unity does not solve the problem.

avatar image RanRanRu · Dec 03, 2011 at 06:18 PM 0
Share

Rechecked the prefab, it's correct.

avatar image Tyler 10 · Dec 03, 2011 at 07:08 PM 0
Share

It is probably instantiating it as a gameobject, not a transform. Cast it as a gameobject, then set obj to the gameobject's transform property.

GameObject g = (GameObject)GameObject.Instantiate(); Transform obj = g.transform;

avatar image RanRanRu · Dec 04, 2011 at 01:26 AM 0
Share

Thanks for the answer. I tried this before, but it didn't work for me.

1 Reply

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

Answer by aldonaletto · Dec 03, 2011 at 07:46 PM

Instantiate returns an object of the same type of the prefab variable. You can change the prefab variable type to Transform:

Transform projectile; // drag the projectile prefab here
...
    Transform obj = Instantiate(projectile,loc,transform.rotation);
The prefab variable type may be equal to any component present in the prefab object, like Rigidbody, Collider, Transform, GameObject etc., thus using GameObject or Transform will work in any case, the only difference being the type returned by Instantiate.
Comment
Add comment · Show 3 · 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 RanRanRu · Dec 04, 2011 at 01:23 AM 0
Share

Thanks, changing the projectile's type to Transform fixes the problem.

I seems that C# is much stricter on types and typecasting than UnityScript. I like it. :)

avatar image aldonaletto · Dec 04, 2011 at 12:19 PM 0
Share

Yes, C# doesn't assume anything to easy your life - everything must be explicitly defined. I consider C# too cranky for a script language, but this helps to avoid some errors. If you know exactly what you're doing, Unityscprit is easier to use; if not, C# is safer - and you learn a lot, because everything is explicit: you can see that each script becomes a new type (a class) with variables and code defined inside, for instance, and many other things that Unityscript does behind the scenes.

avatar image Tyler 10 · Dec 05, 2011 at 05:43 PM 0
Share

In case anyone else searches for this. I ran into the same error for a different reason. I was Instantiating something from a loaded resource like this:

return (GameObject)GameObject.Instantiate((GameObject)Resources.Load(aIdentifier));

And I had I script and a Prefab named the same thing. So unity was defaulting to instantiating the script and (obviously) saying it was an invalid cast.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Behind the scenes voodoo w/ Instantiate 2 Answers

Multiple Cars not working 1 Answer

InvalidCastException when using Instantiate 1 Answer

InvalidCastException C# 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