Hi! i’m starting with Unity, and I have a problem. I have an array of objects (in JS or C#), and an array of cubes in the main stage. Well, I want to create a relation between these two parts. I can add the specific GameObject to each JS or C# in the array, but, in a specific case, I want to select with the mouse one cube in the screen. I can detect that cube with hit.collider, but, how i detect the specific JS or C# object?
I’ve read a lot of stuff about AddComponent() and GetComponent() but I don’t know if this is the answer. I need a method wich helps me to join the cube (GameObject) with the JS or C# object.
Here is the main:
using UnityEngine;
using System.Collections;
// Main de la creació de l'escenari del joc.
public class Main : MonoBehaviour {
double moveSpeed = 1.0;
double turnSpeed = 1.0;
// Variables del tauler de joc.
double ampladaPosicio = 2.0;
double alturaPosicio = 0.5;
double llargadaPosicio = 3.0;
static int llargadaFitxesTauler = 10;
static int ampladaFitxesTauler = 6;
Fitxa [,] tauler = new Fitxa[llargadaFitxesTauler,ampladaFitxesTauler];
Personatge p1;
// Creació de tauler / personatges / cartes de l'usuari / bases.
// Posicionament de la càmara.
void Start () {
float x = -6.302567f;
float y = 14.22612f;
float z = 13.3999f;
Vector3 vector = new Vector3(x,y,z);
transform.position = vector;
for (int fitxesFila = 0; fitxesFila < llargadaFitxesTauler; fitxesFila++) {
for (int fitxesColumna = 0; fitxesColumna < ampladaFitxesTauler; fitxesColumna++) {
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
//cube.AddComponent(Rigidbody);
double posicioX = fitxesColumna * ampladaPosicio;
double posicioZ = fitxesFila * llargadaPosicio;
Vector3 v3 = new Vector3 ((float)posicioX, 0, (float)posicioZ);
cube.transform.position = v3;
v3 = new Vector3 ((float)ampladaPosicio, (float)alturaPosicio, (float)llargadaPosicio);
cube.transform.localScale = v3;
cube.collider.tag = "Fitxa";
tauler[fitxesFila, fitxesColumna] = new Fitxa(fitxesFila, fitxesColumna, (float)posicioX, (float)posicioZ, cube);
// DOUBT HERE cube.AddComponent(Fitxa);
}
}
var figura = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
p1 = new Personatge(figura, tauler[0,0]);
}
void Update () {
/*if(Input.GetButtonDown("Jump")){
transform.position.z += 1.0;
}else if(Input.GetButtonDown("left")){
transform.position.z -= 1.0;
}else if(Input.GetAxis("Horizontal") < 0){
transform.position.x += 1.0;
}else if(Input.GetAxis("Horizontal") > 0){
transform.position.x -= 1.0;
}*/
//Vector3 v = new Vector3(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"), 0);
//Debug.Log(Input.mousePosition.x);
if ( Input.GetMouseButtonDown(0)){
RaycastHit hit = new RaycastHit();
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float marge = 100;
if (Physics.Raycast (ray, out hit, marge)){
if(hit.collider.tag.Equals("Player")){
Debug.Log("Seleccionat");
// GameObject objecte = hit.collider.gameObject;
// Objecte other = objecte.GetComponent("Objecte");
// other.Select();
}
else if(hit.collider.tag.Equals("Personatge")){
Debug.Log("Seleccionat Personatge");
p1.seleccionarPersonatge();
}
else if(hit.collider.tag.Equals("Fitxa")){
GameObject fitxa = hit.collider.gameObject;
int X = (int) fitxa.transform.position.x / 2;
int Z = (int) fitxa.transform.position.z / 3;
p1.mourePersonatge(tauler[Z,X]);
GameObject objecte = hit.collider.gameObject;
// DOUBT HERE Fitxa f = (Fitxa) objecte.GetComponent("Fitxa");
int y = 1;
}
}
}
//
//
// if(Input.GetButton("Forward")){
// transform.position += transform.forward * moveSpeed * Time.deltaTime;
// }
// if(Input.GetButton("Backward")){
// transform.position += - transform.forward * moveSpeed * Time.deltaTime;
// }
// if(Input.GetButton("Left")){
// transform.eulerAngles.y += - turnSpeed * Time.deltaTime;
// }
// if(Input.GetButton("Right")){
// transform.eulerAngles.y += turnSpeed * Time.deltaTime;
// }
}
}
And here the class object I want to join with the GameObject:
using UnityEngine;
using System.Collections;
public class Fitxa : MonoBehaviour {
public int posicioXTauler;
public int posicioYTauler;
public float posicioXmon;
public float posicioYmon;
public GameObject figura;
public Fitxa(){
}
public Fitxa(int xTauler, int yTauler, float xMon, float yMon, GameObject cube){
posicioXTauler = xTauler;
posicioYTauler = yTauler;
posicioXmon = xMon;
posicioYmon = yMon;
figura = cube;
}
}
I will apreciate any help, because I feel like I’m missing really important concepts here. Thanks in advance!
PS: I say “JS or C# object” because I tried with both two languages with no success…