- Home /
Cannot cast from source type to destination type. Can't figure it out!!
Hello, I am making a damage pool and I keep getting this error. I googled it for at least 25 min now and I am getting off track. I need to get back to work as soon as possible so here is the code. The error is pointing to the line where I call `Instantiate
Error:
InvalidCastException: Cannot cast from source type to destination type.
GameController+<spawnDamageTexts>c__Iterator1.MoveNext () (at Assets/Scripts/GameController.cs:52)
UnityEngine.MonoBehaviour:StartCoroutine(String)
GameController:initializeGame() (at Assets/Scripts/GameController.cs:27)
Code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameController : MonoBehaviour
{
[@HideInInspector]
public Queue<GUIText> damageTexts;
public GUIText damageText;
void initializeGame()
{
StartCoroutine("spawnDamageTexts");
}
IEnumerator spawnDamageTexts()
{
//Spawn damage texts over time
while(damgeTextsToSpawn > 0f)
{
GUIText clone = (GUIText)Instantiate(damageText, Vector3.zero, Quaternion.identity);
damageTexts.Enqueue(clone);
yield return new WaitForSeconds(1f);
}
}
}
Answer by nesis · Jan 26, 2014 at 05:27 AM
Try casting it to GameObject, then using GetComponent(), as such:
GameObject o = (GameObject)GameObject.Instantiate(damageText,Vector3.zero,Quaternion.identity);
GUIText clone = o.GetComponent<GUIText>();
The reason your code doesn't work is that Instantiate() returns a reference to the Object created - in this case, a GameObject is created with a GUIText component attached, not a GUIText on its own. So you're trying to cast from GameObject to GUIText, which means your source type (GameObject) is different to your destination type (GUIText) since one doesn't derive its type from the other.
Ok that is a good explanation. Thank you. Now I am getting this error:
NullReferenceException: Object reference not set to an instance of an object
This is on the same line. Here is the updated code:
IEnumerator spawnDamageTexts()
{
//Spawn damage texts over time
while(damgeTextsToSpawn > 0f)
{
GameObject clone = (GameObject)Instantiate(damageText, Vector3.zero, Quaternion.identity);
clone.SetActive(false);
damageTexts.Enqueue(clone);
yield return new WaitForSeconds(1f);
}
}
BTW I need the gameobjects to be inside this queue for the pooling to work in my game. The damageTexts is a queue with type GameObject. I have set my variables to my prefabs in the editor so that is not the problem.
Silly question, but is damageText set in the Inspector? If it's null, you'd get that error.
Yes I set it in the inspector. Still not working :(
I solved the problem. I had to add
damageTexts = new Queue<GameObject>();
before I called the IEnumerator. Thanks!!
Your answer
Follow this Question
Related Questions
Converting float to int in GuiText (javaScript) 3 Answers
Cannot implicitly convert type `float' to `int 1 Answer
I want to change TextMeshProUGUI based on value of a float, but I cant seem to work it. 0 Answers
Is There A Way To Cast On 1 Returning Value 2 Times? 5 Answers
What is wrong with my throttle script? 2 Answers