Quantcast
Viewing latest article 11
Browse Latest Browse All 22

A few C# problems.

Below are two scripts, one for summoning a projectile (goes on the gun), and the other for collisions which goes on the projectile. The first problem is "Cannot cast from source type to destination type." While i have googled the problem none of the answers explain what is wrong but rather give a specific fix. Full debug.log *InvalidCastException: Cannot cast from source type to destination type. WeaponScript.ShootingFunc () (at Assets/Script/WeaponScript.cs:43) WeaponScript.Update () (at Assets/Script/WeaponScript.cs:23)* The second problem i'm having is that every time i fire the Ammo var is supposed to go down by one, however it does not. Also i have a simple delay between shots and this is also not working, is this due to the invalidCastException? This is the code for the weapon and projectile instancing using UnityEngine; using System.Collections; public class WeaponScript : MonoBehaviour { public Rigidbody projectile; public int ammoLeft = 0; public bool isWeaponCycling; public float weaponCycling; void Start () { isWeaponCycling = false; weaponCycling = 5.0f; } void Update () { if (Input.GetButtonDown ("Fire1")) { if (isWeaponCycling == false) { ShootingFunc(); }else { Debug.Log("Cannot Fire at this time"); } } if (isWeaponCycling) { weaponCycling -= Time.deltaTime; if (weaponCycling < 0) { isWeaponCycling = false; weaponCycling = 5.0f; } } } void ShootingFunc () { Rigidbody bullet = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation); isWeaponCycling = true; ammoLeft -= 1; Debug.Log ("You have " + ammoLeft + " rounds left"); if (ammoLeft == 0) { Debug.Log ("Horey shit guys no ammo left"); } } } The other script goes on the actual projectile. public class ProjectileScript : MonoBehaviour { public int projectileSpeed = 500; public float timeTillDeath = 10; void FixedUpdate () { rigidbody.AddForce (Vector3.forward * 10); } void Update () { transform.Translate (Vector3.forward * Time.deltaTime * 600); timeTillDeath = timeTillDeath - Time.deltaTime; if (timeTillDeath < 0) { Destroy (gameObject); } } void OnCollisionEnter(Collision col){ if (col.gameObject.CompareTag("Enemy")){ col.gameObject.SendMessage("HealthAdj", -10, SendMessageOptions.DontRequireReceiver); } Destroy(gameObject); // bullet suicides after hitting anything } } Thanks in advance, this has been bugging me all morning.

Viewing latest article 11
Browse Latest Browse All 22

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>