• 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 /
  • Help Room /
avatar image
0
Question by carlegos · Aug 30, 2019 at 12:49 AM · instantiateexception

I keep getting the Error InvalidCastExceptio and I cant seem to fix it!

I keep getting the Error InvalidCastException: Specified cast is not valid. (wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr) UnityEngine.Object.Instantiate[T] (T original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.bindings.cs:285) FireFireBall.Update () (at Assets/Scripts/FireBall/FireFireBall.cs:24)

Also, it does not seem to turn off the Colliding as well as being fired forward.

//First we make the Prefab we could also do with the game object public GameObject FireBallPrefab;

     public float Thrust = 15f;
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {   //If I left Click
         if (Input.GetMouseButtonDown(0))
             {
                 GameObject FiredObject; //temp Var that we can assign to what ever
 
                 //Instantiate an Object while also Calling it something so that we can referance it later. In this case. We call it FiredObject
                 FiredObject = Instantiate(FireBallPrefab, gameObject.transform.position, gameObject.transform.rotation) as GameObject;
 
                 // we first get the Component from the Fired Object then we tell unity to get it as well to use for the Physics.IgnoreCollision
                 Physics.IgnoreCollision(FiredObject.GetComponent<Collider>(), GetComponent<Collider>()); 
 
                 FiredObject.GetComponent<Rigidbody>().AddForce(gameObject.transform.forward * Thrust);
 
             }
     }
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 carlegos · Aug 30, 2019 at 06:07 PM 0
Share

I replaced the word GameObject with Transform and now it works. But why?

Transform FiredObject; //temp Var that we can assign to what ever

             //Instantiate an Object while also Calling it something so that we can referance it later. In this case. We call it FiredObject
             FiredObject = Instantiate(FireBallPrefab, gameObject.transform.position, gameObject.transform.rotation) as Transform;

             // we first get the Component from the Fired Object then we tell unity to get it as well to use for the Physics.IgnoreCollision
             Physics.IgnoreCollision(FiredObject.GetComponent<Collider>(), GetComponent<Collider>()); 
             
             FiredObject.GetComponent<Rigidbody>().AddForce(gameObject.transform.forward * Thrust);
avatar image I_Am_Err00r carlegos · Aug 30, 2019 at 07:18 PM 0
Share

I couldn't tell you why, but I've also never seen this as GameObject or as Transform line you're adding at the end there:

 FiredObject = Instantiate(FireBallPrefab, gameObject.transform.position, gameObject.transform.rotation) as GameObject;

If you take off that at the end, does it let you use the GameObject?

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

NullReferenceException 0 Answers

Instantiate spawns an empty game object called "New Game Object" as well as a non functional intended game object. 1 Answer

Cannot set parent for instantiated GameObject. (Exception: Can't destroy Transform component of...) 0 Answers

Checking if an instance of a Script saved in 3d array is null 1 Answer

HOW TO INSTANTIATE IMAGE INSIDE CANVAS? 0 Answers


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