Activating Functions in Classes

[Sorry for the duplicate post… I thought UnityAnswers ate my first one]

Hey everyone!

I have an array of custom objects representing tiles on a board. Inside each object is a function to instantiate a prefab. For some reason, this function works well when it’s in a script in the Start() function of my game controller, but not when it’s buried in the class.

When compiled, Unity tells me that it can’t find tilePrefab, TileArray, and it has no clue what Instantiate is.

I know that this is an obvious problem but I can’t quite figure it out.

The stipped-down version of the code in question:

// In GameController.js

public var tilePrefab : Object; 
public var TileArray : Tile[,];

function InitialGridLoadup (){
	tilePrefab = Resources.LoadAssetAtPath("Assets/Prefabs/Tile/Tile.prefab", Object);
		
	for ( var x : int = 0; x < mapX; x++ ) {
		for ( var z : int = 0; z < mapZ; z++ ) {
			// Creates the tile
			TileArray[x, z] = new Tile(type, false, 10, x, z);
			TileArray[x, z].CreateTile();		
		}
	}
}

class Tile{ 
	var type : int;
	var visible : boolean = false;
	var health : int;
	var x : float;
	var z : float;
	
	function CreateTile () {
		switch (type){
		case 0 :
			var tile : Transform = Instantiate(tilePrefab.transform, new Vector3(x,0,z),Quaternion.identity);
			tile.name = TileArray[x, z].Name();
			break;
        }
}

As a super added bonus, does anyone know a streamlined way to replace a GameObject with another prefab at runtime? I need a function here to replace one type of tile with another.

There are a couple of thing wrong with that you are doing. First, tilePrefab needs to have type Transform, not Object, since Object can be a much larger number of things. Instantiate only takes Transform’s, which is why it doesn’t work.

The other problem, with initializing the array, is that you are using the C# style of creating an array, with the T[,] syntax. In JS, Unity will only support having a array containing arrays, not a true multidimensional array.

Also, Tile needs to extend MonoBehavior, to get Instantiate.

Overall, I would recommend starting again in C#. I think it’s syntax will come more naturally to you. Most of this code would translate without many problems.

Edit:

Just for demonstration, here is a C# version of your code:

using UnityEngine;

class Grid : MonoBehaviour {
    public Transform tilePrefab; 
    public Tile[,] TileArray;
	public int mapX;
	public int mapZ;
	public int type;
    
    public void InitialGridLoadup (){
		TileArray = new Tile[mapX,mapZ];
        for ( int x = 0; x < mapX; x++ ) {
            for ( int z = 0; z < mapZ; z++ ) {
                // Creates the tile
                TileArray[x, z] = new Tile() {type = type, visible = false, health = 10, x = x, z = z, tilePrefab = tilePrefab};
                TileArray[x, z].CreateTile();       
            }
        }
    }
    
    public class Tile{ 
        public int type;
        public bool visible = false;
        public int health;
        public float x;
        public float z;
		public Transform tilePrefab;
		
        public void CreateTile () {
            switch (type){
				case 0 :
					Transform tile = (Transform)Instantiate(this.tilePrefab , new Vector3(x,0,z),Quaternion.identity);
					break;
            }
		}
	}
}

(The method that got Name didn’t work, so I couldn’t make a C# version of it.)