Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In Unity i am getting 'Tiletype' is inaccessible due to its protection level in the following underlined lines

Relevant C# Script:

C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class WorldController : MonoBehaviour
{
    public Sprite FloorSprite;
    
    World world;
    // Start is called before the first frame update
    void Start() {
        world = new World();
        // Create a game object for tiles, to see them
        for (int x = 0; x < world.Width; x++ ) {
            for (int y = 0; y < world.Height; y++) {
                Tile tile_data = world.GetTileAt(x, y);
                
                GameObject tile_go = new GameObject();
                tile_go.name = "Tile_" + x + "_" + y;
                tile_go.transform.position = new Vector3( tile_data.X, tile_data.Y, 0);
                
                //add asprite renderer, but no sprites
                tile_go.AddComponent<SpriteRenderer>();
                tile_data.RegisterTileTypeChangeCallback( (tile) => {OnTileTypeChanged(tile, tile_go); } );
            }
        }
    world.RandomizeTiles();
    }
    
    float randomizeTileTimer = 2f;

    // Update is called once per frame
    void Update()
    {
        randomizeTileTimer = Time.deltaTime;
        if(randomizeTileTimer < 0) {
            world.RandomizeTiles();
            randomizeTileTimer = 2f;
        }
    }
    
    void OnTileTypeChanged(Tile tile_data, GameObject tile_go) {
        if(tile_data.type == Tile.TileType.Floor) {
            tile_go.GetComponent<SpriteRenderer>().sprite = FloorSprite;
        }
        else if (tile_data.type == Tile.TileType.empty) {
            tile_go.GetComponent<SpriteRenderer>().sprite = null;
        }
        else {
            Debug.LogError("OnTileTypeChanged - Unrecognized tile type.");
        }
    }
}


Second C# Script:

using UnityEngine;
using System.Collections;
using System;

public class Tile {
    
    public enum TileType {empty, Floor };
    
    TileType type = TileType.empty;

    
    Action<Tile> TileTypeChanged;

    public TileType Type {
        get {
            return type;
        }
        set {
            type = value;
            // call the call back
            if(TileTypeChanged != null)
                TileTypeChanged(this);
        }
    }
    
    StandaloneObject standaloneObject;
    InstalledObject installedObject;
    
    World world;
    int x;
    
    public int X {
        get {
            return x;
        }
    }
    int y;
    
    public int Y {
        get {
            return y;
        }
    }
    public Tile (World world, int x, int y ) {
        this.world = world;
        this.x = x;
        this.y = y;
    }
    public void RegisterTileTypeChangeCallback(Action<Tile> callback) {
        TileTypeChanged += callback;
    }
    public void UnRegisterTileTypeChangeCallback(Action<Tile> callback) {
        TileTypeChanged -= callback;
    }
}


What I have tried:

From what i know ive done it correctly but its not working, its most likely that i made a mistake. please help me out
Posted
Updated 20-Dec-22 22:52pm
Comments
Chris Copeland 21-Dec-22 4:34am    
Did you not mean to refer to tile_data.Type instead? Remember that C# is case sensitive, you're trying to access the type field which is inaccessible; you instead need to refer to the Type property.
Richard Deeming 21-Dec-22 4:38am    
Sounds like the answer to me. :)

1 solution

Chris is almost certainly right and has given you the solution.

But, I'd just like to add a bit of background: You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!

And spending a little time learning to understand syntax error messages will save you a huge amount of time in future: you waited at least 1/4 hour for Chris to reply, then your email system probably added another 10 minutes or so, plus the time it took you to type up the question once you had found this site and created an account. Chances are that you could have saved a significant chunk of that time if you knew how to read them!

I'm not saying we don't want to help you fix them - sometimes I can't see my own errors because I read what I meant to write - but fixing syntax errors is part of the job, and if you can't do it for yourself people are going to look at you as a bit weird should you get a job in the industry!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900