Error CS0246 The type or namespace name ‘Tile_container’ could not be found (are you missing a using directive or an assembly reference?)
In the tutorial I’m referencing, a prefab name is used as a type in a script. But when I try to do it, I get an error. Am I missing something? I suppose I could just use GameObject as the type, but I’d like to do it the right way if possible.
I’m assuming that as this is not a Unity type, then it must be one that they are creating at some point in the tutorial.
Perhaps if you can provide the link to the tutorial we might be able to see where it is set up.
Sorry about that. I should have been more specific about what was happening. I’m using “tile_container”, but in the tutorial, he’s using “Tile”. In episode 1, 14:36 he creates a prefab called Tile.
Then in episode 4, 13:43 he creates an array, “public Tile[ ] NextTiles”.
This type of thing is not working for me. I got a type/namespace error, but the only difference was I was using tile_container instead of Tile.
Then where he has created the Tile.cs script with the class Tile in it, you should have created the script as Tile_container.cs with the class called Tile_container
If you haven’t created the script and class as Tile_container, then you can’t just start using Tile_container in place of Tile.
Have you actually created the script and class Tile_container?
Yes, I created the tile_container prefab in the Unity editor, just as he created the Tile prefab in the Unity editor. I’m doing exactly what he did, as far as I can tell.
This is his code. The name of the script class is Tile and also the name of the tile container prefab created in the Unity editor is called Tile.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tile : MonoBehaviour
{
// Use this for initialization
void Start()
{
}
public Tile[] NextTiles;
public PlayerStone PlayerStone;
public bool IsScoringSpace;
public bool IsRollAgain;
public bool IsSideline; // Is part of a player's private/safe area
// Update is called once per frame
void Update()
{
}
}
I just assumed that it was referring to the prefabs, not the name of the script, which doesn’t make sense to me.
Well that’s your problem. When you reference types/classes in C# you are referring to the classes in the scripts. Therefore you cannot just type Tile_container because it won’t realise that you actually want it to go and use a class called Tile. You will need to either:
Change your script file to be named Tile_container and change the class in the script from Tile to be Tile_container
OR
Change anywhere you are trying to use Tile_container in your scripts and instead put Tile just like in the tutorial.
It does not matter what you call the prefab. I could follow that tutorial and call the tile prefab “wibble-donk” for all it cares.
You need to keep in mind that Unity is a component-based system, ie: everything you add to your GameObject is a component (Rigidbody, Colliders, etc) and that includes scripts. So if you were to use the FindObjectOfType function (eg: FindObjectOfType<Tile>() ) function it will look for a GameObject that has that specific component (eg: Tile) on it. So no matter what you call the prefab, if it has a script called Tile that contains the Tile class then that GameObject will be of type Tile. And of course adding multiple scripts, each with their own class in them, will mean that the GameObject will be of each of those types (eg: a player GameObject might have PlayerHealth, PlayerCombatManager, PlayerMovement, etc. - so that one GameObject could be found when searching for any of those class types).
Thank you for looking into this and explaining about classes, GameObjects, FindObjectOfType, and so forth. I was stuck on the prefab-as-a-class notion, but now I understand the issue. Back on track and moving forward…