- Home /
NullReferenceException....Again(sorry)
I have scoured the internet and answers to help get this figured out but I'm still stuck. I'm just starting out and feel bad, as this question seems to have been answered many times. ANYWAY...
I'm trying to have my level be spawned, in relation to the player(they will be "digging" downward). I want each layer to have its own color and that color to be created randomly. So I've tried to create an array in the worldBuild script to save the values "on the fly" and the probability script to to take those values and feed it to the renderer to change the color(the probability code is on the Cube prefab that I'm instantiating in worldBuild). So here are the questions
1- Why am I getting the NullReferenceException on line 34 of probability?(I thought that I referenced the component(script) correctly...)
2- The code is not changing color for anything but the renderer is there.(checked by renderer.material.color = color.cyan;)
3- As a noobie to coding in general and even more so c#, am I approaching this incorrectly?
Any at all help would be greatly appreciated. Thanks folks.
Code here (an incredible mess from trying to fix it so many times)
public class worldBuild : MonoBehaviour {
public Transform Cube;
public Transform Player;
public int layer;
public float[,] layerList;
public float[,] getLayerListCol()
{
layerList[(Mathf.Abs(layer)), 0] = Random.value;
layerList[(Mathf.Abs(layer)), 1] = Random.value;
layerList[(Mathf.Abs(layer)), 2] = Random.value;
layerList[(Mathf.Abs(layer)), 3] = 1.0f;
return layerList;
}
public float[,] updateLayerList()
{
layerList = new float[50, 4];
return layerList;
}
public void make()
{
for (layer = 0; layer > Player.transform.position.y - 10; layer--)
{
for (int x = 0; x < 50; x++)
{
Instantiate(Cube, new Vector3(x, layer, 0), Quaternion.identity);
}
}
}
void Start()
{
Instantiate(Player, new Vector3(25, 1, 0), Quaternion.identity);
updateLayerList ();
}
// Update is called once per frame
void Update ()
{
if (layer > (Player.transform.position.y - 10))
make ();
getLayerListCol ();
}
}
public class probability : MonoBehaviour
{
public Transform Cube;
public GameObject theNittyGritty;
private Color changingColor;
private worldBuild colorList;
public float colR;
public float colG;
public float colB;
public float colA;
public int yVal;
public float[,] layerList;
// Use this for initialization
void Start ()
{
int yVal = (int) Cube.transform.position.y;
int odds = Random.Range (1, 100);
if(odds < Mathf.Abs(yVal))
Destroy(gameObject);
colorList = theNittyGritty.GetComponent<worldBuild> ();
layerList = colorList.layerList;
colR = layerList[Mathf.Abs (yVal), 0];
colG = layerList[Mathf.Abs (yVal), 1];
colB = layerList[Mathf.Abs (yVal), 2];
colA = layerList[Mathf.Abs (yVal), 3];
changingColor = new Color(colR, colG, colB, colA);
renderer.material.color = changingColor;
}
// Update is called once per frame
void Update ()
{
}
void OnDestroy()
{
DestroyImmediate (renderer.material);
}
}
Your answer
Follow this Question
Related Questions
NullReference Problem with pragma strict 1 Answer
NullReferenceException when using GetComponent to access script variables 1 Answer
DLL NullReferenceException 1 Answer
script GetComponent, nullReference error 1 Answer
Has my project's data corrupted? Script keeps returning null but 20 minutes ago it wasn't? 1 Answer