Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MrProfessorTroll · Jan 26, 2014 at 05:21 AM · floatguitextcastqueue

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);
         }
     }
 
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrProfessorTroll · Jan 26, 2014 at 05:39 AM 0
Share

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.

avatar image nesis · Jan 26, 2014 at 01:26 PM 1
Share

Silly question, but is damageText set in the Inspector? If it's null, you'd get that error.

avatar image MrProfessorTroll · Jan 26, 2014 at 09:51 PM 0
Share

Yes I set it in the inspector. Still not working :(

avatar image MrProfessorTroll · Jan 26, 2014 at 10:27 PM 0
Share

I solved the problem. I had to add

 damageTexts = new Queue<GameObject>();

before I called the IEnumerator. Thanks!!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges