Is there a way to refer to the only script the object has without naming the script?
I am making a clicker game where you can progress in several different upgrade paths (around 20). I have written one generic "buyUpgrade" script, which checks if a certain upgrade is buyable, subtracts the cost from the resources and raises the level of the upgrade path by 1.
I want to send this level from buyUpgrade (which is attached to several different upgrade path objects) to a different object for each different upgrade path (most upgrade function scripts work differently and are on several different game objects). I cannot do this via dragging the object in the inspector to get the upgrade function script because all upgrade function scripts are different and have different names. I can of course get the object this way but I need afterwards a way to generically refer to the only script a GameObject has.
Originally I had an "affectedObject" GameObject variable in the buyUpgrade script to which I sent the level via SendMessage. This was the perfect solution until I realized that it is not possible to send messages to inactive objects. I am a bit lost now how to solve this situation. Any help appreciated.
EDIT: clarity
Answer by tormentoarmagedoom · Jun 12, 2018 at 03:08 PM
Good day.
I think you can declare a variable of type "Object". And later assign to that object any Component Script. So this variable can have any of the scripts. The problem is how to find the script in the object without saying its name... I think is not possible, to find it from "buyUpgrade" script. What you can do, is that every of these scripts, go to find this "Object" variable inside "buyUpgrade", and say:
The ObjectVariable = this;
So Its all of these diferent scripts who define itselfs in the buyUpgrade script.
Bye!!
Answer by Tuonela · Jun 12, 2018 at 05:07 PM
Hey, thanks for the answer. I tried what you suggested: I declared an object variable in the buyUpgrade script and defined it as "this" in the upgrade function script in Awake (). However I cannot change this object's variables from buyUpgrade, the variables declared in the upgrade function script aren't recognized.
Why I want to send the variable from buyUpgrade instead of retrieving it from the particular upgrade function is I thought it would be not optimal for the performance if I call the level from the cached buyUpgrade, perform some calculations on it, update UI texts etc. each frame, since I am not able to check when it has changed. When I send from buyUpgrade I only do all that when the upgrade is bought. I don't know if it is expensive to do all that every frame/click... Considering that I will have ~20 upgrades in the end.
Your answer
Follow this Question
Related Questions
How to expose other components fields 0 Answers
Unity can't see any scripts 0 Answers
Reference to Array Attribute from Inspector 0 Answers
How do I remove ALL components from a gameObject? 3 Answers
Custom inspector and GameObject Children 0 Answers