Dynamicly getting an object? Please help!

Hello, So i have recently created this script below which im using to fire a raycast at a collider and depending on what the colliders randomly set name is then it will complete the an if statement depending on its name,… i was wondering is their a more Dynamic way of doing this rather then typing If all the time ?? - Thanks in Advance if anyone can give me a hand it would be much appreciated…

using UnityEngine;
using System.Collections;

public class ChestIDdetect : MonoBehaviour {
    public Collider coll;
    private int i;
    // Use this for initialization
    void Start () {
     
    }
 
    // Update is called once per frame
    void Update () {

        if (Input.GetMouseButtonDown (1)) {
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            RaycastHit hit;
            if (coll.Raycast (ray, out hit, 4.0F)) {
                Debug.Log ("HitChest : "+coll.name);
                if(coll.name == "Chestid1"){
                    GameObject Bank = GameObject.Find ("Chest1");
                    var bank1 = Bank.GetComponent<BankUI>();
                    bank1.window.Show();
                }else if(coll.name == "Chestid2"){
                    GameObject Bank2 = GameObject.Find ("Chest2");
                    var bank2 = Bank2.GetComponent<BankUI>();
                    bank2.window.Show();
                }else if(coll.name == "Chestid3"){
                    GameObject Bank3 = GameObject.Find ("Chest3");
                    var bank3 = Bank3.GetComponent<BankUI>();
                    bank3.window.Show();
                }else if(coll.name == "Chestid4"){
                    GameObject Bank4 = GameObject.Find ("Chest4");
                    var bank4 = Bank4.GetComponent<BankUI>();
                    bank4.window.Show();
                }else if(coll.name == "Chestid5"){
                    GameObject Bank5 = GameObject.Find ("Chest5");
                    var bank5 = Bank5.GetComponent<BankUI>();
                    bank5.window.Show();
                }else if(coll.name == "Chestid6"){
                    GameObject Bank6 = GameObject.Find ("Chest6");
                    var bank6 = Bank6.GetComponent<BankUI>();
                    bank6.window.Show();
                }else if(coll.name == "Chestid7"){
                    GameObject Bank7 = GameObject.Find ("Chest7");
                    var bank7 = Bank7.GetComponent<BankUI>();
                    bank7.window.Show();
                }else if(coll.name == "Chestid8"){
                    GameObject Bank8 = GameObject.Find ("Chest8");
                    var bank8 = Bank8.GetComponent<BankUI>();
                    bank8.window.Show();
                }else if(coll.name == "Chestid9"){
                    GameObject Bank9 = GameObject.Find ("Chest9");
                    var bank9 = Bank9.GetComponent<BankUI>();
                    bank9.window.Show();
                }else if(coll.name == "Chestid10"){
                    GameObject Bank10 = GameObject.Find ("Chest10");
                    var bank10 = Bank10.GetComponent<BankUI>();
                    bank10.window.Show();
                }
            }
        }
    }
}

im on phone but put one instance of your code and use col1.name to act as your switch.

GameObject Bank = GameObject.Find (col1.name.substring (0, 5) + col1.name.substring(7, 1));

var bank = Bank.GetComponent<BankUI>();

bank.window.Show();

Hey Thanks so much for the Reply :slight_smile: i Posted what i did Below but just a heads up ive never used Substrings before so i have no idea what they do ect… But i put it in like you see below and it seems to work fine Up until it reaches the 9th chests after that it dosent do anything? :S i have a feeling its got something to do with the Substrings but yet again i have no clue what they do or how they work… Again thanks for the Help this is so much Easyer then manually doing If statements everytime i want a new chest :'D …

using UnityEngine;
using System.Collections;

public class ChestIDdetect : MonoBehaviour {
    public Collider coll;
    private int i;
    // Use this for initialization
    void Start () {
      
    }
  
    // Update is called once per frame
    void Update () {

        if (Input.GetMouseButtonDown (1)) {
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            RaycastHit hit;
            if (coll.Raycast (ray, out hit, 4.0F)) {
                Debug.Log ("HitChest : "+coll.name);
                    GameObject Bank = GameObject.Find (coll.name.Substring (0, 5) + coll.name.Substring(7, 1));
                    var bank = Bank.GetComponent<BankUI>();
                    bank.window.Show();
            }
        }
    }
}

EDIT* ok so i changed the colliders like you said to Col1 and now i get 10 !! my bad i did not read your answer correctly :S … Also im Developing a voxel game so the amount of chests that can be created is essentially Endless how would i go about making the number bigger then 10 ? so i can create as many as i like ? - Thanks in advance!

You should probably link the UI object with the collider object through some script instead of using Find. If you put a script like this on the same object as your collider:

public class BankUILinker : MonoBehaviour {

    public BankUI linkedUI;

}

And assign the linkedUI field in the inspector to the correct BankUI, you can simply replace your code with this:

        if (Input.GetMouseButtonDown (1)) {
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            RaycastHit hit;
            if (coll.Raycast (ray, out hit, 4.0F)) {
                BankUILinker linker = hit.collider.GetComponent<BankUILinker>();
                if(linker != null) { //Will be null if what your raycast hit is not a chest
                    linker.linkedUI.window.Show();
                }
            }
        }
    }
}

This is both faster, and easier to manage.

1 Like

Thanks for Replying!, so i tryed your Version of what could work and im not sure it would work the way i need it to the object with a collider is a prefab so i cant place the Bank gameobject in the linkedUI, also the problem still seemed to Exist i would have to somehow grab each bank window depending on what chest was clicked… The code in the Prev post works Fine but it uses Substrings and im not Exactly sure how they work ive tryed googling it because if i can figure out how they work i would be able to make it so its not Restricted to 10 but i have no idea how they work and cant seem to find alot of information on them. Again Thanks for the help :slight_smile: And if you happen to know anything about substrings feel free to Inform me becaue i have never used them before haha :slight_smile: