0

I have a complicated issue. Here is some of my code, which is a class for game tiles.

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

namespace game_tiles {
    public class GameTile {

        public GameObject tilePrefab;
        public bool isPassable;
        public float moveCost;
    }

    public class GameTiles : MonoBehaviour {

        public GameTile redTile;
        public GameObject redTilePrefab;
        public bool redTileIsPassable;
        public float redTileMoveCost;

        public void Main() {

            GameTile redTile = new GameTile();
            redTile.tilePrefab = redTilePrefab;
            redTile.isPassable = redTileIsPassable;
            redTile.moveCost = redTileMoveCost;
            print(redTile);
        }
    }
}

When the above code runs, it prints to the unity console: game_tiles.GameTile

Completely normal, right?

However, I want to use the code in a different file, using this code here:

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

namespace board_manager {
    public class BoardManager : MonoBehaviour {

        GameTiles gameTiles;

        // Use this for initialization
        void Start() {

            gameTiles = GameObject.Find("Game Tile Class").GetComponent<GameTiles>();

            GameTile redTile = gameTiles.redTile;
            print(redTile);  
        }
    }
}

This code, for some reason, prints Null to the Unity console, and, thus, I cannot use any of the variables in the redTile object. How can I get both programs to print game_tiles.GameTile? Thank you for any help.

P.S. Sorry about all of the similar gameTile variables. I could not think of different names. d=

1 Answer 1

2

You defined a new variable in the constructor, when you wanted to use the class-level variable it looks like:

public class GameTiles : MonoBehaviour {

    public GameTile redTile;
    public GameObject redTilePrefab;
    public bool redTileIsPassable;
    public float redTileMoveCost;

    public void Main() {

        this.redTile = new GameTile();
        this.redTile.tilePrefab = redTilePrefab;
        this.redTile.isPassable = redTileIsPassable;
        this.redTile.moveCost = redTileMoveCost;
        print(this.redTile);
    }
}

Notice the constructor now uses this. It's not required but used to illustrate that we're using the global variable, not creating our own (var redTile created a local copy that doesn't propagate automatically up to the class).

Sign up to request clarification or add additional context in comments.

3 Comments

I'm sorry that I do not quite understand how this works, but it did. Thank you so much. Maybe I will research this more to figure out how it works!
This might sound bizarre, but recently I moved all my code to an Ubuntu computer from Windows 10, and now, even with your answer, the new code still returns null! It is not working any more, even though I reproduced your answer exactly. Is this a known problem, or am I doing something fundamentally wrong? As I said, at the time, your answer was just fine.
Never mind It was just a simple problem with execution order. I fixed that by changing the execution order of the scripts. lol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.