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.