Unity Resources.Load vs as Sprite

前端 未结 2 1606
清酒与你
清酒与你 2021-01-18 17:25

I\'ve tried to change the image of my object with this code (used as Sprite cast):

GetComponent().sprite = Resources.L         


        
2条回答
  •  误落风尘
    2021-01-18 17:53

    Resources.Load("GameObjects/Tiles/Hole") as Sprite;
    

    You have another "Hole" in your Resources folder. This other-Hole is not a Sprite. Therefore when you use as Sprite it simply can't be casted to one and won't throw an exception (on that line) because:

    The as operator is like a cast operation. However, if the conversion isn't possible, as returns null instead of raising an exception.


    Resources.Load("GameObjects/Tiles/Hole");
    

    In the working code you specify which file you want, the Sprite, so it finds the correct one.

提交回复
热议问题