I’m prototyping a simple game. Cubes representing the chemical elements are randomly distributed in a plane. Each has a texture which has the element symbol My character is another cube that moves in that plane. The idea is to touch each element cube in turn, in the correct order. The element cubes are instantiated in an array in the Start() method of an empty GameManager object. I’m trying to detect collisions (triggers) with a script on the element cubes, but I’m having trouble checking to see if any given cube is the next one in the array. I suspect I’m not using the right references.
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour
{
public static int nextElement = 0;
public static GameObject[] elements ;
public int numElements = 5;
void Start ()
{
elements = new GameObject[numElements];
GameObject element = Resources.Load("Element") as GameObject; //the cube prefab
for(int i=0;i<elements.Length;i++)
{
float x = Random.Range(-20,20);
float z = Random.Range(-20,20);
elements[i] = Instantiate(element, new Vector3(x,0,z),Quaternion.identity) as GameObject;
elements[i].renderer.material.mainTexture = Resources.Load((i+1).ToString()) as Texture;
}
}
}
using UnityEngine;
using System.Collections;
public class Element : MonoBehaviour
{
void OnTriggerEnter(Collider col)
{
print(gameObject);
if(gameObject == GameManager.elements[GameManager.nextElement])
{
print(GameManager.nextElement);
GameManager.nextElement++;
}
}
}
using UnityEngine;
using System.Collections;
public class Character : MonoBehaviour
{
public float speed = 0.1f;
// Update is called once per frame
void Update ()
{
float x = Input.GetAxis("Horizontal")*speed;
float z = Input.GetAxis("Vertical")*speed;
transform.position += new Vector3(x, 0, z);
}
}
The character has a RigidBody, the Element cubles have their Box Collider set to Trigger. The code is currently printing Element(Clone) multiple times due to the print statement before the conditional.
OK, some more checking. It does actually print the value of nextElement from inside the if, except for the first one (value 0). So, having commented the print before the condition, when I run the game and hit the H cube nothing is printed, but if I then hit the He cube 1 is printed, the Li cube and 2 is printed. If I hit out of order nothing is printed. So the code seems to work because the static nextElement is incremented correctly, even the first time. Just the value is not printed. Very strange.
using UnityEngine;
using System.Collections;
public class Element : MonoBehaviour
{
void OnTriggerEnter(Collider col)
{
if(gameObject == GameManager.elements[GameManager.nextElement])
{
print("in if " + GameManager.nextElement);
GameManager.nextElement++;
}
}
}
More testing, more confusion. I’ve added code to line up the cubes when they are touched. All blocks line up except the first!! So, when the nextElement is 0 print statements don’t work, translations don’t work, but incrementing the variable does work!!
using UnityEngine;
using System.Collections;
public class Element : MonoBehaviour
{
void OnTriggerEnter(Collider col)
{
if(gameObject == GameManager.elements[GameManager.nextElement])
{
//print("in if " + GameManager.nextElement);
transform.position = new Vector3(GameManager.nextElement, 0,0);
collider.enabled = false;
GameManager.nextElement++;
}
}
}
all the blocks line up except the first one. However they won’t line up if I don’t touch the first one first, so the nextElement variable is obviously being incremented correctly. Just the block won’t move.
When I have problems like this, I usually find I have changed the value in the editor, which is what Unity uses even if it’s been initialized differently.