- Home /
Retrieve a variable from another script dynamically with a variable with GetComponent<>
Is it possible to dynamically use GetComponent to retrieve a variable (VideoClip) with another string variable? Say I would want to change the video clip of a video player in another game object, but all video clips are stored in another script. So if I had video1, video2, and video3 in the "VideoManager" game object, and I would want to retrieve those VideoClip variables via a string (videoName) instead of an explicit variable name (video1), how could I go about that? Let me know if I need to add onto this quesion.
Example:
string videoName = "video1"; GameObject.Find("VideoManager").GetComponent().videoName;
Does not work
I would like this to be equivalent to...
GameObject.Find("VideoManager").GetComponent<VideoManager>().video1;
Works
Any help or pointers would be useful!
I don't think that's possible. See here: Dynamically add fields to class You'd be better off adding the videos to a Video[]
or string[] and using an index to access them. For example: videos[0] = "video0"
@squidee_
Answer by awsapps · Jun 20, 2021 at 12:15 PM
Make an array of videos and access them via its index
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
create a dynamic menu 1 Answer