- Home /
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.
I assume "line 71" corresponds to line 49 in the above script?
Actually, line 71 was an empty line before if (shooting2 && Time.time > nextFire2)
, which I found strange.
Sorry, yes line 71 does correspond to line 49 using the forums script line count.
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.
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;
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?
Your answer
Follow this Question
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