- Home /
Access renderer from transform
Hi guys,
I have been trying to access the Renderer from a Transform that I have instantiated. The error is "Object reference not set to an instance of an object", and it appeared at the GridEnable and GridDisable functions, where I called the tile.gameObject.renderer.enabled.
This is the GridScript, which control the grid in a 2D plane:
using UnityEngine;
using System.Collections;
public class GridScript : MonoBehaviour {
public int gridWidth = 9;
public int gridHeight = 9;
public int remainingGrid;
public int totalGrid;
public Transform[,] grid;
public int[,] gridStatus;
public int[,] gridBorder;
private PlayerControl playerControl;
// Use this for initialization
void Awake ()
{
playerControl = GameObject.FindWithTag ("Player").GetComponent<PlayerControl>();
}
public void GridEnable(Transform tile)
{
tile.gameObject.renderer.enabled = true;
int x = Mathf.RoundToInt (tile.localPosition.x);
int y = Mathf.RoundToInt (tile.localPosition.y);
gridStatus [x, y] = 0;
}
public void GridDisable(Transform tile)
{
tile.gameObject.renderer.enabled = false;
int x = Mathf.RoundToInt (tile.localPosition.x);
int y = Mathf.RoundToInt (tile.localPosition.y);
gridStatus [x, y] = 1;
}
}
And This is the GridSetup, which used to generate the grid from a text file
using UnityEngine;
using System.Collections;
public class GridSetup : MonoBehaviour {
public int gridHeight = 9;
public int gridWidth = 9;
public Transform normalTile;
public TextAsset textFile;
private GridScript gridScript;
private string text;
// Use this for initialization
void Awake () {
gridScript = GetComponent<GridScript>();
text = textFile.text;
CreateGrid (text, gridScript);
}
// Translate a text file into a map of grid, store in gridScript
void CreateGrid (string text, GridScript gridScript) {
// Initialize the grid
gridScript.gridHeight = gridHeight;
gridScript.gridWidth = gridWidth;
gridScript.grid = new Transform[gridWidth + 2, gridHeight + 2];
gridScript.gridStatus = new int[gridWidth + 2, gridHeight + 2];
gridScript.gridBorder = new int[gridWidth + 2, gridHeight + 2];
gridScript.totalGrid = gridHeight * gridWidth;
int xx = 0;
int yy = gridHeight;
foreach (char c in text)
{
if (c == '\r') continue;
if (c == '\n') {
xx = 0;
yy--;
}
else {
xx++;
gridScript.grid[xx, yy] =
Instantiate(normalTile, new Vector3((float)xx, (float)yy, 8f), Quaternion.identity) as Transform;
if (c == 'x') {
gridScript.GridDisable(gridScript.grid[xx, yy]);
gridScript.totalGrid--;
}
else
{
gridScript.GridEnable(gridScript.grid[xx, yy]);
}
}
}
gridScript.remainingGrid = gridScript.totalGrid;
}
}
Can you try and format your scripts properly so it's easier to read. Also can you share the whole script, as from this people can't tell if you are even calling the function at all.
Sorry for the bad format. I have posted the two scripts. Hope it can help :)
The error your getting is because you are trying to access a "GameObject" from your tile transform and there isn't one, transform are not gameobjects and only gameobjects have renderers
So is there any other ways rather than create an array of GameObjects ins$$anonymous$$d of Transform?
Answer by Calum-McManus · Mar 05, 2014 at 01:31 PM
You are setting up a load of Transforms in your scene that form a grid, nothing in these scripts are visible. you would have to Instantiate gameobjects to form the grid not transforms.
$$anonymous$$any thanks to Calum.$$anonymous$$acmanus, I have fixed my code. The problem is because I instantiate Transform ins$$anonymous$$d of Gameobject.
The
gridScript.grid[xx, yy] =
Instantiate(normalTile, new Vector3((float)xx, (float)yy, 8f), Quaternion.identity) as Transform;
should be changed to
gridScript.grid[xx, yy] =
(Instantiate(normalTile, new Vector3((float)xx, (float)yy, 8f), Quaternion.identity) as GameObject).transform;
If you accept my answer it will close the question, glad I could help :)
If Calum solved your problem, please mark his answer as the solution
Your answer
Follow this Question
Related Questions
Adding a prefab to gameObject at a certain position in runtime 0 Answers
Creating new Transform from existing objects Transform to Instantiate object 1 Answer
Animation issue (OnTriggerEnter) ***SOLVED*** 1 Answer
move a GameObject based on Input.GetTouch 1 Answer
Scale (Transform.localscale) a gameobject based on ARFaceAnchor anchorData 0 Answers