- Home /
Detect if object is child, from extern script
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.
Answer by octabond · Nov 11, 2015 at 05:50 PM
I found the solution. The key was on the clic() function. Instead of looking for the child, i looked for its parent.
if (Physics.Raycast(ray, out hit, 100)){
if(hit.transform.parent.GetComponent<boton>() != null)
{
Debug.Log("from findables");
hit.transform.parent.GetComponent<boton>().nombre(hit.transform);
}
}
Note that for the Boton() function, I passed the transform of the hit so it could know from which child of the parent was invoked. If not done like this, the function would destroy all the objects from the parent.
public void nombre(Transform t){
if(t.name=="findable1"){
Destroy(t.gameObject);
PlayerPrefs.SetInt ("item1",1);
}
if(t.name=="findable2"){
Destroy(t.gameObject);
PlayerPrefs.SetInt ("item2",1);
}
if(t.name=="findable3"){
Destroy(t.gameObject);
PlayerPrefs.SetInt ("item3",1);
}
if(t.name=="findable4"){
Destroy(t.gameObject);
PlayerPrefs.SetInt ("item4",1);
}
if(t.name=="findable5"){
Destroy(t.gameObject);
PlayerPrefs.SetInt ("item5",1);
}
if(t.name=="findable6"){
Destroy(t.gameObject);
PlayerPrefs.SetInt ("item6",1);
}
if(t.name=="findable7"){
Destroy(t.gameObject);
PlayerPrefs.SetInt ("item7",1);
}
if(t.name=="findable8"){
Destroy(t.gameObject);
PlayerPrefs.SetInt ("item8",1);
}
}
Oh, and I also change the repeated variables assignations in changesprite(), since I just copy-pasted them and forgot to change it in my original post (in my code they were different since the beginning).
Hope this can help anyone with the same problem.