This looks like it should be easy but i just cant get it to work, ![]()
I am trying to send a bool from one script to another script in the same game object.
Thank you in advance ![]()
-unitPower
This looks like it should be easy but i just cant get it to work, ![]()
I am trying to send a bool from one script to another script in the same game object.
Thank you in advance ![]()
-unitPower
Easy way is to set a variable of the script type and use GetComponent ( Unity - Scripting API: GameObject.GetComponent ) to save the other script name in the Start method (or assign it in the inspector)
then you can just access it directly
eg
OtherScriptType otherScriptVar;
.
.
.
otherScriptVar = gameObject.GetComponent(OtherScriptType);
.
.
.
otherScriptVar.MethodYouWantToRun(valueYouWantToSend);
SendMessage already sends to all MonoBehaviours on the object; you don’t need to use GetComponent.
–Eric
If it is something that is happening repeatedly isnt it better to use GetComponent initially and use the saved variable rather than repeatedly using SendMessage though? Also if you use SendMessage and have the same method name in different scripts (for whatever reason) it will trigger all of them?
Yes, that’s the idea. I wouldn’t be concerned with performance unless it’s something you’re doing every frame maybe.
–Eric
I feel comfortable working with GetComponent but because of the nature of scripts, it will be a lot easier to able to send a message.