Tree spawn script not working

Hi, these are some script I’ve been working on. They are giving me quite a hard time :frowning:

using UnityEngine;
using System.Collections;

public class Sapling : MonoBehaviour {

	private int WaitTime;
	public int MinWaitTime;
	public int MaxWaitTime;
	private float TimeWaited;
	private bool HasGrown;
	public GameObject CubeGet;
	public Sprite [] Sprites = new Sprite [2];
	
	void Start () {
		WaitTime = Random.Range (MinWaitTime, MaxWaitTime + 1);
	}
	
	void Update () {
		if (TimeWaited < WaitTime)
		{
			TimeWaited += 1 * Time.deltaTime;
		}
		else
		{
			if (HasGrown == false)
			{
				Grow ();
				HasGrown = true;
			}
		}
	}
	void Grow () {
		int LogHeight = Random.Range (1, 5);
		for (int LogsSpawned = 0; LogsSpawned < LogHeight + 1; LogsSpawned ++)
		{
			GameObject CurrentLog = Instantiate (CubeGet, transform.position + Vector3.up * LogsSpawned, Quaternion.identity) as GameObject;
			CurrentLog.GetComponent <Cube> ().Type = Cube.Types.Wood;
		}
		int LeafHeight = Random.Range (2, 4);
		GameObject CurrentLeaf = Instantiate (CubeGet, transform.position + Vector3.up * (LogHeight + 1) + Vector3.left, Quaternion.identity) as GameObject;
		CurrentLeaf.GetComponent <Cube> ().Type = Cube.Types.Leaf;
		CurrentLeaf = Instantiate (CubeGet, transform.position + Vector3.up * (LogHeight + 1), Quaternion.identity) as GameObject;
		CurrentLeaf.GetComponent <Cube> ().Type = Cube.Types.Leaf;
		CurrentLeaf = Instantiate (CubeGet, transform.position + Vector3.up * (LogHeight + 1) + Vector3.right, Quaternion.identity) as GameObject;
		CurrentLeaf.GetComponent <Cube> ().Type = Cube.Types.Leaf;
		CurrentLeaf = Instantiate (CubeGet, transform.position + Vector3.up * (LogHeight + 2), Quaternion.identity) as GameObject;
		CurrentLeaf.GetComponent <Cube> ().Type = Cube.Types.Leaf;
		if (LeafHeight == 2)
		{
			Destroy (gameObject);
		}
		if (LeafHeight == 3)
		{
			CurrentLeaf = Instantiate (CubeGet, transform.position + Vector3.up * (LogHeight + 2) + Vector3.left, Quaternion.identity) as GameObject;
			CurrentLeaf.GetComponent <Cube> ().Type = Cube.Types.Leaf;
			CurrentLeaf = Instantiate (CubeGet, transform.position + Vector3.up * (LogHeight + 2) + Vector3.right, Quaternion.identity) as GameObject;
			CurrentLeaf.GetComponent <Cube> ().Type = Cube.Types.Leaf;
			CurrentLeaf = Instantiate (CubeGet, transform.position + Vector3.up * (LogHeight + 3), Quaternion.identity) as GameObject;
			CurrentLeaf.GetComponent <Cube> ().Type = Cube.Types.Leaf;
			Destroy (gameObject);
		}
	}
}

This ^ is a sapling script, that spawns one of several presets of trees after the timer passes a random time.

I think the way I wrote the leaf spawn part may be related to the fact that I can’t inhabit leaves. In my game, you can play as any block by clicking on that block. The leaf blocks can be inhabited, but the color is not right. When I click on a leaf block, It becomes black, instead of getting a tint of red, which is what I wanted.

The wood blocks work fine, however, which is why I find this very weird.

I did in fact check if the block wasn’t being assigned the enum, but it was. “Type” is “Types.Leaf”, yet my code doesn’t tint the leaves red when clicked on.

In fact, in the inspector that item still says its the previous color until i move the block, and then it says it’s red. However, it is not red. It is black.

Here’s the cube script:

using UnityEngine;
using System.Collections;

public class Cube : MonoBehaviour {

	public GameObject [] Managers;
	public int DataValue;
	public enum Types {
		Cube,
		Wood,
		Leaf
	};
	public Types Type;
	public GameObject ButtonGet;
	private bool IsMoving;
	private bool MouseOver;
	public Sprite [] Sprites = new Sprite [3];

	void Start () {
		Managers [0].GetComponent <CubeManager> ().Cubes.Add (gameObject);
		DataValue = Managers [0].GetComponent <CubeManager> ().Cubes.Count - 1;
		if (Type == Types.Cube)
		{
			if (DataValue == 0)
			{
				GetComponent <SpriteRenderer> ().sortingOrder = 20;
				GetComponent <SpriteRenderer> ().color = Color.red;
			}
			else
			{
				GetComponent <SpriteRenderer> ().sortingOrder = 0;
				GetComponent <SpriteRenderer> ().color = Color.black;
			}
		}
		else
		{
			GetComponent <SpriteRenderer> ().color = Color.white;
		}
		if (Type == Types.Wood)
		{
			GetComponent <SpriteRenderer> ().sortingOrder = 1;
			GetComponent <SpriteRenderer> ().sprite = Sprites [1];
		}
		if (Type == Types.Leaf)
		{
			GetComponent <SpriteRenderer> ().sortingOrder = 2;
			GetComponent <SpriteRenderer> ().sprite = Sprites [2];
		}
		gameObject.name = Type + " (" + DataValue + ")";
	}

	void Update () {
		if (Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.D)) 
		{
			if (IsMoving == false && Managers [0].GetComponent <CubeManager> ().CurrentCube == DataValue) 
			{
				StartCoroutine (Move ());
			}
		}
		else
		{
			StopCoroutine (Move ());
		}
		if (MouseOver == true && Input.GetMouseButton (1) && Managers [0].GetComponent <CubeManager> ().CurrentCube != DataValue)
		{
			Destroy (gameObject);
		}
	}

	IEnumerator Move () {
		IsMoving = true;
		if (IsMoving == true)
		{
			if (Input.GetKey (KeyCode.W))
			{
				transform.position += Vector3.up;
				if (Camera.main.transform.position.y < transform.position.y - 3)
				{
					Camera.main.transform.position += Vector3.up;
				}
			}
			if (Input.GetKey (KeyCode.A))
			{
				transform.position += Vector3.left;
				if (Camera.main.transform.position.x > transform.position.x + 5)
				{
					Camera.main.transform.position += Vector3.left;
				}
			}
			if (Input.GetKey (KeyCode.S))
			{
				transform.position += Vector3.down;
				if (Camera.main.transform.position.y > transform.position.y + 3)
				{
					Camera.main.transform.position += Vector3.down;
				}
			}
			if (Input.GetKey (KeyCode.D))
			{
				transform.position += Vector3.right;
				if (Camera.main.transform.position.x < transform.position.x - 5)
				{
					Camera.main.transform.position += Vector3.right;
				}
			}
		}
		yield return new WaitForSeconds (0.16f);
		IsMoving = false;
	}
	
	void OnMouseOver () {
		MouseOver = true;
	}
	
	void OnMouseExit () {
		MouseOver = false;
	}
	
	void OnMouseDown () {
		if (Managers [0].GetComponent <CubeManager> ().Cubes [Managers [0].GetComponent <CubeManager> ().CurrentCube].GetComponent <Cube> ().Type == Types.Cube)
		{
			Debug.Log ("Black");
			Managers [0].GetComponent <CubeManager> ().Cubes [Managers [0].GetComponent <CubeManager> ().CurrentCube].GetComponent <SpriteRenderer> ().color = Color.black; 
		}
		else
		{
			Managers [0].GetComponent <CubeManager> ().Cubes [Managers [0].GetComponent <CubeManager> ().CurrentCube].GetComponent <SpriteRenderer> ().color = Color.white; 
		}
		Managers [0].GetComponent <CubeManager> ().CurrentCube = DataValue;
		GetComponent <SpriteRenderer> ().color = Color.red;
		
	}
}

Also all Cube Manager is is just a container for all cubes. It hold a generic list for all blocks, knows the current cube’s data value, etc. However, it has nothing to do with this.

To prove it, here is the CubeManager script:

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

public class CubeManager : MonoBehaviour {

	public GameObject CubeGet;
	public List <GameObject> Cubes = new List <GameObject> ();
	public int CurrentCube;
	public int MinCubes;
	public int MaxCubes;
	public int MinX;
	public int MaxX;
	public int MinY;
	public int MaxY;

	void Start () {
		int CubesToSpawn = Random.Range (MinCubes, MaxCubes + 1);
		for (int CubesSpawned = 0; CubesSpawned < CubesToSpawn + 1; CubesSpawned ++) 
		{
			Instantiate (CubeGet, new Vector2 (Random.Range (MinX, MaxX + 1), Random.Range (MinY, MaxY + 1)), Quaternion.identity);
		}
	}
}

I also already checked if I assigned the wrong thing, but I wouldn’t have because I would’ve gotten a null reference exception if I had assigned the wrong manager.

I hope the community can help me, thank you very much to all of you in advance.

I’m not quite sure exactly what you are asking, but I can help a bit with the color problem. When placing a red filter over a green block, the red filter only lets through red light. The green block does not have any red light, its green. The green light can’t go through the filter, and because there is no red light, the block appears black. The easiest way to fix this (if you wrote your own code for the color changing) is to allow some green and blue light through. instead of multiplying the color value by (1,0,0) try something like (1,0.6,0.6). Then you will probably see something more like this instead of black.

alt text

Good luck.