I’m starting on unity and I have some very simple grid code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class GridManager : MonoBehaviour
{
private float[,] Grid;
private int Vertical, Horizontal, Columns, Rows;
public GameObject tile;
// Start is called before the first frame update
void Start()
{
Vertical = (int) Camera.main.orthographicSize;
Horizontal = Vertical * (Screen.width / Screen.height);
Columns = Horizontal * 2;
Rows = Vertical * 2;
Grid = new float[Columns, Rows];
for (int i = 0; i < Columns; i++)
{
for (int j = 0; j < Rows; j++)
{
Grid[i, j] = Random.Range(0.0f, 1.0f);
SpawnTile(i,j, Grid[i,j]);
}
}
}
private void SpawnTile(int x, int y, float value)
{
Instantiate(tile);
tile.transform.position = new Vector3(x - (Horizontal - 0.5f), y - (Vertical - 0.5f));
Debug.Log(tile.GetComponent<tile>().objName.ToString());
}
// Update is called once per frame
void Update()
{
}
}
I have my tile prefab attached to my GridManager game object in unity and all is going well except I cannot log objName. I’m not sure why the code to access the tile’s variable isn’t working. Here is the tile script attached to my tile prefab:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class tile : MonoBehaviour
{
public Sprite sprite;
public int objName;
// Start is called before the first frame update
void Start()
{
objName = Random.Range(0, 990000000);
}
// Update is called once per frame
void Update()
{
}
}
Again, I am new to unity and from what I’ve read tile.GetComponent<tile>().objName.ToString() should be how I access other scripts variables. It would be very helpful if you could point out where I am messing up. Thank you in advance.
GameObject tile = Instantiate(tile) as GameObject;
finished
public transform prefab;
private void SpawnTile(int x, int y, float value)
{
Vector3 newPosition = new Vector3(x - (Horizontal - 0.5f), y - (Vertical - 0.5f));
GameObject tile = Instantiate(prefab, newPosition , Quaternion.identity) as GameObject;
tile.GetComponent<tile>().objName = Random.Range(0, 990000000);
Debug.Log(tile.GetComponent<tile>().objName.ToString());
}
if your using visual studio use file/open folder navigate to your unity project folder and it should load everything as a project and it will give you more help on things that are wrong.
Put the objName line in Awake(), not Start. If you call another script from Start(), you can’t be certain the other script’s Start() has been called yet.
Also,
private void SpawnTile(int x, int y, float value)
{
GameObject temp = Instantiate(tile);
temp.transform.position = new Vector3(x - (Horizontal - 0.5f), y - (Vertical - 0.5f));
Debug.Log(temp.GetComponent<tile>().objName);
}
Notice I used temp to get the returned instantiated object, whereas you just instantiated it and then it’s lost…but you still accessed it with “tile” and it works…can anyone speak to this? Doesn’t seem like it’s the right way to deal with instantiated objects, but I might be missing something.