Newbie here, with excuses in advance if it was already answered or explained in some documents. Evidently I can’t get it, even after reading severals posts and API’s GetComponent() manual pages, I’ve solved nothing and I’m now more confused.
I have different Gameobjects with scripts on them all named like Cube1_MyScript, Cube2_MyScript, and so on.
I would like to access scripts passing their names as string variable to GetComponent(), if feasible. Something like this:
public class Test : MonoBehaviour {
public Myscript script;
void Update() { [cut]
objectname = hit.collider.name;
scriptname = objectname+"_MyScript"; // In order to have Cube1_MyScript
script = GameObject.Find(objectname).GetComponent<scriptname>();
script.MyVarBool = true;
}
This code obiouvsly doesn’t work, just like several other that I’ve wrote, unfortunately I can’t figure out alone if there’s any way to get over this.
Thanks in advance for pointing out any info or help - will be very appreciated.