I am trying to make a VR find objects game. There is a GUI with shaded objects to be found. When you touch an object on screen and the object is one of the findable objects, the object dissapears and appears in your GUI as colored.
I´ve already implemented this, but I think I´ve been too much redundant since I am pasting the same script in all of my findable objects. So what I´m trying to achieve is to attach the script to a parent object so when the object is clicked it can check into the childrens and delete them from the parent.
Here are my scripts:
public class clic : MonoBehaviour {
void Start(){
PlayerPrefs.SetInt ("item1", 0);
PlayerPrefs.SetInt ("item2", 0);
PlayerPrefs.SetInt ("item3", 0);
PlayerPrefs.SetInt ("item4", 0);
PlayerPrefs.SetInt ("item5", 0);
PlayerPrefs.SetInt ("item6", 0);
PlayerPrefs.SetInt ("item7", 0);
PlayerPrefs.SetInt ("item8", 0);
}
void Update () {
if(Input.GetMouseButtonDown(0) ){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100)){
if(hit.transform.IsChildOf(transform))
{
hit.transform.GetComponent<boton>().nombre();
}
/*if(hit.transform.GetComponent<boton>() != null){
hit.transform.GetComponent<boton>().nombre();
}*/
}
}//-----fin metodo onmousedown
if (Input.GetKeyDown ("space")) {
PlayerPrefs.SetInt ("item1", 0);
PlayerPrefs.SetInt ("item2", 0);
}
}
}
Clic() goes in the camera
public class boton : MonoBehaviour {
//public GameObject findable1,findable2,findable3,findable4,findable5,findable6,findable7,findable8;
public void nombre(){
gameObject.GetComponentsInChildren<GameObject> ();
if(this.name=="findable1"){
Destroy(gameObject);
PlayerPrefs.SetInt ("item1",1);
}
if(this.name=="findable2"){
Destroy(gameObject);
PlayerPrefs.SetInt ("item2",1);
}
if(this.name=="findable3"){
Destroy(gameObject);
PlayerPrefs.SetInt ("item3",1);
}
if(this.name=="findable4"){
Destroy(gameObject);
PlayerPrefs.SetInt ("item4",1);
}
if(this.name=="findable5"){
Destroy(gameObject);
PlayerPrefs.SetInt ("item5",1);
}
if(this.name=="findable6"){
Destroy(gameObject);
PlayerPrefs.SetInt ("item6",1);
}
if(this.name=="findable7"){
Destroy(gameObject);
PlayerPrefs.SetInt ("item7",1);
}
if(this.name=="findable8"){
Destroy(gameObject);
PlayerPrefs.SetInt ("item8",1);
}
}
}
boton() goes in the findable objects (now parent object with all findable children)
public class changeSprite : MonoBehaviour {
public Sprite itemColored1,itemColored2,itemColored3,itemColored4,itemColored5,itemColored6,itemColored7,itemColored8;
public Image itemShaded1,itemShaded2,itemShaded3,itemShaded4,itemShaded5,itemShaded6,itemShaded7,itemShaded8;
void start(){
itemShaded1 = GameObject.Find ("item1_shaded").GetComponent<Image>();
itemShaded2 = GameObject.Find ("item2_shaded").GetComponent<Image>();
itemShaded3 = GameObject.Find ("item1_shaded").GetComponent<Image>();
itemShaded4 = GameObject.Find ("item2_shaded").GetComponent<Image>();
itemShaded5 = GameObject.Find ("item1_shaded").GetComponent<Image>();
itemShaded6 = GameObject.Find ("item2_shaded").GetComponent<Image>();
itemShaded7 = GameObject.Find ("item1_shaded").GetComponent<Image>();
itemShaded8 = GameObject.Find ("item2_shaded").GetComponent<Image>();
}
void Update () {
//ebug.Log (PlayerPrefs.GetInt("item1_shaded")+"-----"+PlayerPrefs.GetInt("item2_shaded"))
if (PlayerPrefs.GetInt ("item1") == 1) {
itemShaded1.sprite = itemColored1;
}
if (PlayerPrefs.GetInt ("item2") == 1) {
itemShaded2.sprite = itemColored2;
}
if (PlayerPrefs.GetInt ("item3") == 1) {
itemShaded1.sprite = itemColored3;
}
if (PlayerPrefs.GetInt ("item4") == 1) {
itemShaded2.sprite = itemColored4;
}
if (PlayerPrefs.GetInt ("item5") == 1) {
itemShaded1.sprite = itemColored5;
}
if (PlayerPrefs.GetInt ("item6") == 1) {
itemShaded2.sprite = itemColored6;
}
if (PlayerPrefs.GetInt ("item7") == 1) {
itemShaded1.sprite = itemColored7;
}
if (PlayerPrefs.GetInt ("item8") == 1) {
itemShaded2.sprite = itemColored8;
}
}
}
ChangeSprite() goes in the camera as well.
When run, the script does nothing.
I would appreciate if someone could help me on this.