简体   繁体   中英

Cannot cast from source type to destination type iterating transform

I'm fiddling around with a day/night cycle script in C# that I'm making. I have no idea if it is any good, I'm just trying things out in C#, since I'm new to it. I think I've got a pretty decent code here, the Debug doesn't say anything until I test out the game. When I test it, it'll say:

InvalidCastException: Cannot cast from source type to destination type. cycleFlow.DayNightCycle () (at Assets/Scripts/cycleFlow.cs:28) cycleFlow.Update () (at Assets/Scripts/cycleFlow.cs:14)

This is what I have got:

using UnityEngine;
using System.Collections;

public class cycleFlow : MonoBehaviour {

  private Color night;
  private Color day;

  void Start () {

    night [0] = 30;
    night [1] = 30;
    night [2] = 30;

    day [0] = 255;
    day [1] = 255;
    day [2] = 255;

  }

  void Update () {
    DayNightCycle ();
  }

  void DayNightCycle()
  {

    foreach (SpriteRenderer child in transform)
      if(Input.GetKeyDown(KeyCode.Q))
        child.color = Color.Lerp(child.color, night, Time.deltaTime);

    foreach (SpriteRenderer child in transform)
      if(Input.GetKeyDown(KeyCode.E))
        child.color = Color.Lerp(child.color, day, Time.deltaTime);

  }
}

What's going on? (First time posting here btw, sorry if I do anything wrong)

Try this modification simply to get past your error:

foreach (SpriteRenderer child in transform.GetComponentsInChildren<SpriteRenderer>())

Going further I'd try to keep in mind framerate. You may want to cache these renderers if you can from your Start function. Additionally, I'd move the if(Input.GetKeyDown(KeyCode.E)) to be outside your loop instead of inside.

EDIT: Coming back to this I also notice you're only acting on key down, rather than at all times while the key is pressed. (See difference between GetKeyDown , GetKey , and GetKeyUp ). You might try something like this:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class cycleFlow : MonoBehaviour
{

  private Color night;
  private Color day;
  private IEnumerable<SpriteRenderer> childSpriteRenderers;

  void Start ()
  {
    night = new Color(30, 30, 30);
    day = Color.white;

    childSpriteRenderers = transform.GetComponentsInChildren<SpriteRenderer> ();
  }

  void Update ()
  {
    DayNightCycle ();
  }

  void DayNightCycle ()
  {
    if (Input.GetKey (KeyCode.Q))
    {
      foreach (SpriteRenderer child in childSpriteRenderers)
      {
        child.color = Color.Lerp (child.color, night, Time.deltaTime);
      }
    }

    if (Input.GetKey (KeyCode.E))
    {   
      foreach (SpriteRenderer child in childSpriteRenderers)
      {
        child.color = Color.Lerp (child.color, day, Time.deltaTime);
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM