Search Unity

The type or namespace name 'Tile_container' could not be found

Discussion in 'Getting Started' started by MarkSteere, Mar 6, 2023.

  1. MarkSteere

    MarkSteere

    Joined:
    Dec 31, 2022
    Posts:
    54
    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.
     
  2. MarkSteere

    MarkSteere

    Joined:
    Dec 31, 2022
    Posts:
    54
    Code (CSharp):
    1.     public Tile_container Setup_Tile;
    2.  
     
  3. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    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.
     
  4. MarkSteere

    MarkSteere

    Joined:
    Dec 31, 2022
    Posts:
    54
    Hi, thanks for responding. The tutorial is Unity 3D: Making a board game - Episode 4.

     
  5. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    I dont see anything about tile_container anywhere in that episode. Are you sure it is that one where they get you to put it in one of the scripts?
     
  6. MarkSteere

    MarkSteere

    Joined:
    Dec 31, 2022
    Posts:
    54
    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.
     
  7. MarkSteere

    MarkSteere

    Joined:
    Dec 31, 2022
    Posts:
    54
  8. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    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
    ?
     
  9. MarkSteere

    MarkSteere

    Joined:
    Dec 31, 2022
    Posts:
    54
    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.
     
  10. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    It doesn't matter what the prefab is called, it is the script and class name that matters.

    Show us your Tile_container script.
     
  11. MarkSteere

    MarkSteere

    Joined:
    Dec 31, 2022
    Posts:
    54
    Ok, I'm out now, but few hours later. Thanks for looking into this.
     
  12. MarkSteere

    MarkSteere

    Joined:
    Dec 31, 2022
    Posts:
    54
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Tile : MonoBehaviour
    6. {
    7.  
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.        
    12.     }
    13.  
    14.     public Tile[] NextTiles;
    15.     public PlayerStone PlayerStone;
    16.     public bool IsScoringSpace;
    17.     public bool IsRollAgain;
    18.     public bool IsSideline; // Is part of a player's private/safe area
    19.    
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.        
    24.     }
    25. }
    I just assumed that it was referring to the prefabs, not the name of the script, which doesn't make sense to me.
     
  13. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    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:

    1. Change your script file to be named
    Tile_container
    and change the class in the script from
    Tile
    to be
    Tile_container

    OR
    2. 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).
     
  14. MarkSteere

    MarkSteere

    Joined:
    Dec 31, 2022
    Posts:
    54
    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...