[英]GameObjects showing up in Hierachy, but are invisible in the camera
I was following this tutorial on creating a 2D chessboard.我正在按照本教程创建 2D 棋盘。 Everything kinda worked, my squares are showing up as GameObject in the Hierachy, but I can't see them in the camera, only their hitboxes.
一切正常,我的方块在 Hierachy 中显示为 GameObject,但我在相机中看不到它们,只有它们的命中框。 I double checked everything, but everything seems right.
我仔细检查了一切,但一切似乎都是正确的。 Maybe there is a setting, which wasn't mentioned in the tutorial, which i have to change?
也许有一个设置,在教程中没有提到,我必须改变? If any additional data/information is needed, let me know (all code is below).
如果需要任何其他数据/信息,请告诉我(所有代码如下)。
This is the scene when pressing play:这是按下播放时的场景:
This is my square prefab:这是我的方形预制件:
And the GameManager gameObject:和 GameManager 游戏对象:
Board.cs: Board.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Board : MonoBehaviour
{
public GameObject tilePrefab;
public GameObject pawnPrefab, knightPrefab, bishopPrefab, rookPrefab, queenPrefab, kingPrefab;
public Material whiteMat, blackMat;
public Material whitePieceMat, blackPieceMat;
public GameObject[,] squares = new GameObject[8, 8];
[HideInInspector]
public static string[] alphabet = new string[] {"a", "b", "c", "d", "e", "f", "g", "h"};
public void CreateBoard()
{
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
squares [i, j] = Instantiate (tilePrefab, new Vector3 (i, j, 0), Quaternion.identity);
if (i % 2 != 0 && j % 2 != 0 || i % 2 == 0 && j % 2 == 0)
{
squares [i, j].GetComponent<Renderer>().material = blackMat;
}
else
{
squares [i, j].GetComponent<Renderer>().material = whiteMat;
}
squares [i, j].transform.SetParent (gameObject.transform);
squares [i, j].name = alphabet [i] + (j + 1);
}
}
}
}
GameManager.cs:游戏管理器.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
private Board board;
void Start()
{
board = gameObject.GetComponent<Board>();
board.CreateBoard();
}
}
i have had a similar issue in 3D and here is few tips for things you may want to look for;我在 3D 中遇到过类似的问题,这里有一些您可能想要寻找的提示; 1: the clipping of the camera - If the clipping is too small the gameobject wont show to the camera when rendered 2: the normals of the sprite - If the normal is facing the other way of the camera the sprite will be invisible
1:摄像机的裁剪 - 如果裁剪太小,渲染时游戏对象不会显示给摄像机 2:精灵的法线 - 如果法线朝向摄像机的另一侧,则精灵将不可见
Make sure the material is ok, this would be one of the reasons why the objects are not visible.确保材料没问题,这将是物体不可见的原因之一。 As #Hacky said, the code looks correct, it would be ok to give us more information, some screenshots with the hierarchy and prefab.
正如#Hacky 所说,代码看起来是正确的,可以给我们更多信息,一些带有层次结构和预制的屏幕截图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.