Question by
ChrisKirit · Jun 12, 2017 at 08:44 AM ·
c#errorinvalidcastexception
InvalidCastException: Cannot cast from source type to destination type.
I'm having a huge issue, as a unity noob i've looked over the already completed answers but I have no idea how to apply what fixes they've found to what I'm doing.
This is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAttak : MonoBehaviour {
[SerializeField]
float delayBetweenShots = 0.4f;
float timePassedSinceLast = 0f;
// Use this for initialization
void Start() {
timePassedSinceLast = delayBetweenShots;
}
// Update is called once per frame
void Update () {
Aiming();
Shooting();
}
void Aiming()
{
var objectPos = Camera.main.WorldToScreenPoint(transform.position);
var dir = Input.mousePosition - objectPos;
transform.rotation = Quaternion.Euler (new Vector3 (0, 0, Mathf.Atan2 (-dir.x, dir.y) * Mathf.Rad2Deg));
}
void Shooting()
{
if(Input.GetMouseButton(0) && timePassedSinceLast >= delayBetweenShots)
{
GameObject dagger = (GameObject)Instantiate(Resources.Load("dagger"), transform.position, transform.rotation);
timePassedSinceLast = 0f;
}
else
{
timePassedSinceLast += Time.deltaTime;
}
}
}
And this is the error
InvalidCastException: Cannot cast from source type to destination type.
PlayerAttak.Shooting () (at Assets/scripts/PlayerAttak.cs:35)
PlayerAttak.Update () (at Assets/scripts/PlayerAttak.cs:20)
Comment
Answer by berto_dev · Jun 12, 2017 at 07:10 PM
i have the same probleb, with the same code, dagger is a sprite
Your answer
Follow this Question
Related Questions
InvalidCastException: Cannot cast from source type to destination type. 1 Answer
InvalidCastException: Cannot cast from source type to destination type. 2 Answers
InvalidCastException: Cannot cast from source type to destination type. 1 Answer
Setting EventTrigger in Unity with JavaScript 0 Answers
GameObject has undefined tag! 0 Answers