- Home /
Accessing a script only using its string
Looked around but couldn't find the answer. I've got a function in my script which generates a random number. I want to use that number to access a variable in the script of the according object by turning the int into a string. I've got Objects obj1 with scriptName1, obj2 with scriptName2 etc. and a list of the objects in my main script. I tried doing
string stringS = "scriptName" + randomNumber.ToString(); var1 = objectList[randomNumber].GetComponent(stringS).variable;
but that does not work unless I reference it without using the string like this
var1 = objectList[randomNumber].GetComponent<scriptName1>().variable;
Is there any possible way to use the string for the reference? Thank you for the help.
i think i can help you but i need some extra info, each object in the list have all the different classes scriptName1 scriptName2 etc...? if not, use a base object scriptNameBase and make the rest childs so you can use
var1 = objectList[randomNumber].GetComponent().variable;
@xxmariofer Each object only has one script attached to it, all of them numbered. The main script is in the parent object. If I understood your instructions correctly, when I attempt to assign the variable I get this error on the console: Using the generic method UnityEngine.GameObject.GetComponent<T>()' requires
1' type argument(s) Cheers
im saying something like this: //parent public abstract class ParentScript : $$anonymous$$onobehaviour { public int variable }
//child example public class scriptName1 : ParentScript { //your stuff }
//reference that script var1 = objectList[randomNumber].GetComponent().variable;
couldnt test it
Answer by tormentoarmagedoom · Jan 15, 2019 at 04:24 PM
Good day.
I'm sure it is posible, but I've never done to find a script, only to find variables inside a script using a string. I will do simple and "stupid" example (C#) to show you how I think it can work. If works or not, please come to explain what you get.
string RandNumb = "2"
Lets look for the object called "SuperObject2":
GameObject ObjectSelected = GameObject.Find("SuperObject"+RandNumb );
Now lets acces its Script, which is called "MyScript2":
Object ScriptIWillUse = ObjectSelected.GetType("MyScript"+RandNum);
Bye!
@tormentoarmagedoom Thanks for the reply. Tried this but when I use GetType I get an error: No overload for method GetType takes 1 argument.
Answer by sean244 · Jan 16, 2019 at 03:14 AM
You can do this
string stringS = "scriptName" + randomNumber.ToString();
var otherScript = objectList[randomNumber].GetComponent(stringS);
var myVariable = otherScript.variable;
Answer by cdr9042 · Jan 16, 2019 at 04:07 AM
Can you explain why you have to do it this way and not another way? Normally if there are many scripts that do the similar things I would make them inherit from one class.
For example
public class Vehicle{
protected virtual void Go() {
}
}
public class Bike : Vehicle {
protected override void Go() {
}
}
public class Car : Vehicle {
protected override void Go() {
}
}
Then if I want to pick randomly between the car and the bike, and then run the function Go()
Vehicle pickVehicle;
void PickRandomThenGo(){
GameObject randomObj = PickRandomObj(); //get the random object
pickVehicle = randomObj.GetComponent<Vehicle>();
pickVehicle.Go();
}
Like that, if the pickVehicle was of Bike class, the function Go() inside Bike class will run. Otherwise, Car's function will run.