-2

This basically sets up the tiles for my endless runner, the error in particular being:

Inconsistent accessibility: field type 'gameobject' is less accessible than tilePrefabs

enter image description here

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

public class TileManager : MonoBehaviour {

    private Transform playerTransform;
    private float spawnZ = 0.0f;
    private float tileLength = 39.0f;
    private int amnTileOnScreen = 7;

    public GameObject[] tilePrefabs;
    // ** it is already public here **

    private List<GameObject> activeTiles;
    // Use this for initialization
    public void Start () {
        activeTiles = new List<GameObject> ();

        playerTransform = gameObject.FindGameObjectWithTag("Player").transform;
        for (int i = 0; i <= amnTileOnScreen; i++) {
            SpawnTile ();
        }
    }  

    private void Update () {
        if (playerTransform.position.z > (spawnZ - amnTileOnScreen + 
         tileLength)) {
            SpawnTile ();
            //DisableTile ();
            DestroyTile();
        }
    }

    public void SpawnTile(int prefabIndex = - 1)
    {
        GameObject go;
        go = Instantiate(tilePrefabs[0]) as GameObject;

        // reappears here

        go.transform.SetParent(transform);
        go.transform.position = Vector3.forward*spawnZ;
        spawnZ += tileLength;
        activeTiles.Add (go);
    }

    private void DestroyTile()
    {
        Destroy (transform.GetChild (0));
    }
}
Jérémie Bertrand
  • 3,025
  • 3
  • 44
  • 53
sunsetfox
  • 1
  • 2
  • 2
    Are you able to give us the EXACT error message, including the correct case? You question states the error message uses the name 'gameobject', but there's no 'gameobject' in your code. There's also going to be a problem with your code. You're adding Instantiated objects to your private `activeTiles` list, but then destroying the object without removing the object from the `activeTiles` list. That would be a runtime issue though. In this case, a `Queue` would probably be better. And most definitely recycling your objects if possible (pooling). – Milan Egon Votrubec Jun 29 '18 at 01:49
  • 1
    Since we don't know what the error is it is hard to help. However, there are a few things that concern me. Firstly you use an array for tilePrfabs, but a list for activeTiles. Is there any reason why this isn't uniform? Another thing I would recommend is rather than have an array of tile prefabs, just have a public variable for each prefab. This is how I've done it with my tile based games. Of course if you have a very large number of tiles this may not be convenient. – pseudoabdul Jun 29 '18 at 02:46

1 Answers1

2

Here is where the problem is located:

playerTransform = gameObject.FindGameObjectWithTag("Player").transform;

There is a difference between gameObject and GameObject. Notice the capitalized "G" in the second one.

GameObject is just a class used to create GameObjects.

gameObject is a variable that is created from GameObject as is declared as public GameObjects gameObject in Unity's Component class. It's simply an instance of GameObjects.

You get access to the gameObject variable when your TileManager script derives from MonoBehaviour.


The FindWithTag function is in the GameObject class and is also declared as static which means that you don't need instance of GameObject to call it. You have to call it directly with the class name as

Replace

gameObject.FindGameObjectWithTag("Player").transform;

with

GameObject.FindGameObjectWithTag("Player").transform;
Programmer
  • 121,791
  • 22
  • 236
  • 328