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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by whelpton · Jul 31, 2013 at 04:00 PM · c#instantiateinvalidcastexception

InvalidCastException: Cannot cast from source type to destination type.

Hello. I'm sorry to be a noob, but I have very little experience in Unity scripting. Can someone please point me in the right direction as to why I'm getting a InvalidCastException error on line 71 of this?

 using UnityEngine;
 using System.Collections;
 
 public class Shot : MonoBehaviour
 {
     public Transform projectilePrefab;
     public Transform projectilePrefab2;
     public float fireRate = 0.5f;
     private float nextFire = 0.0f;
     public float fireRate2 = 0.1f;
     private float nextFire2 = 0.0f;
     private bool shooting = false;
     private bool shooting2 = false;
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetButtonDown("Fire1"))
             shooting = true;
         if (Input.GetButtonUp("Fire1"))
             shooting = false;
         if (Input.GetButtonDown("Fire2"))
             shooting2 = true;
         if (Input.GetButtonUp("Fire2"))
             shooting2 = false;
 
         if (shooting && Time.time > nextFire)
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit, 100000.0f) && hit.collider.gameObject.tag != "PlayerShip")
             {
                 GameObject projectile = (GameObject)Instantiate(projectilePrefab, transform.position + transform.forward * 8, transform.rotation);
                 projectile.transform.LookAt(hit.point);
             }
             else
             {
                 GameObject projectile = (GameObject)Instantiate(projectilePrefab, transform.position + transform.forward * 8, transform.rotation);
                 projectile.transform.LookAt(ray.GetPoint(300.0f));
             }
             nextFire = Time.time + fireRate;
         }
         if (shooting2 && Time.time > nextFire2)
         {
             Ray ray2 = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit2;
             if (Physics.Raycast(ray2, out hit2, 100000.0f) && hit2.collider.gameObject.tag != "PlayerShip")
             {
                 GameObject projectile2 = (GameObject)Instantiate(projectilePrefab2, transform.position + transform.forward * 8, transform.rotation);
                 projectile2.transform.LookAt(hit2.point);
             }
             else
             {
                 GameObject projectile2 = (GameObject)Instantiate(projectilePrefab2, transform.position + transform.forward * 8, transform.rotation);
                 projectile2.transform.LookAt(ray2.GetPoint(300.0f));
             }
             nextFire2 = Time.time + fireRate2;
         }
     }
 }

I have searched the manuals and googled the issue for a long time now, but I just can't figure out why I'm coming on this problem.

Thanks very much.

Comment
Add comment · Show 5
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 perchik · Jul 31, 2013 at 07:32 PM 0
Share

I assume "line 71" corresponds to line 49 in the above script?

avatar image Jamora · Jul 31, 2013 at 07:53 PM 0
Share

Actually, line 71 was an empty line before if (shooting2 && Time.time > nextFire2), which I found strange.

avatar image perchik · Jul 31, 2013 at 07:56 PM 0
Share

Can you provide the full text of the error?

avatar image whelpton · Jul 31, 2013 at 07:57 PM 0
Share

Sorry, yes line 71 does correspond to line 49 using the forums script line count.

avatar image perchik · Jul 31, 2013 at 08:04 PM 0
Share

I'd still like to see the full text of the error

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Jamora · Jul 31, 2013 at 04:01 PM

You are trying to cast a Transform into a GameObject. This will not work. Use GameObject instead of Transform in lines 13 & 15.

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 whelpton · Jul 31, 2013 at 04:21 PM 0
Share

Thanks for the response, unfortunately I'm still getting the error with

 public class Shot : $$anonymous$$onoBehaviour
 
 {
 
 
 
     public GameObject projectilePrefab;
 
     public GameObject projectilePrefab2;
 
     public float fireRate = 0.5f;
 
     
avatar image Jamora · Jul 31, 2013 at 05:23 PM 0
Share

I compacted your code so I did have to keep scrolling up and down.

It's strange that you get the error and I don't... Have you assigned an actual prefab in the inspector window to the projectilePrefabs?

avatar image whelpton · Jul 31, 2013 at 07:13 PM 0
Share

I certainly have!

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

17 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

Related Questions

Distribute terrain in zones 3 Answers

InvalidCastException when using Instantiate 1 Answer

InvalidCastException C# 1 Answer

Multiple Cars not working 1 Answer

C# Extending Script causes InvalidCastException on 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