Honn
Honn

Reputation: 744

GameObjects showing up in Hierachy, but are invisible in the camera

I was following this tutorial on creating a 2D chessboard. 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. 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 the scene when pressing play

This is my square prefab: enter image description here enter image description here

And the GameManager gameObject: enter image description here

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:

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

public class GameManager : MonoBehaviour
{
    private Board board;
    void Start()
    {
        board = gameObject.GetComponent<Board>();
        board.CreateBoard();
    }
}

Upvotes: 1

Views: 108

Answers (2)

1961777
1961777

Reputation: 46

i have had a similar issue in 3D and here is few tips for things you may want to look for; 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

Upvotes: 2

Gheorghe Ciusca
Gheorghe Ciusca

Reputation: 57

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.

Upvotes: 1

Related Questions