accessing a script using a variable with getComponent, then accessing a variable inside that script
Hello. I'd like to know if something I am attempting is actually possible (C#):
MonoBehaviour monobehaviour; int theCapacity; theCapacity = combineMag1.GetComponent ().currentCapacity;
I'd like it to be so that i can assign a variable to access a specific script, and then use that variable to access a variable inside that script. If anyone knows how please help, thanks
Answer by UnityCoach · Aug 02, 2017 at 11:03 AM
Yes, simply use
YourComponentType comp = GetComponent<YourComponentType>();
comp.yourVariable = the value;
or all on one line if you're sure the component is there.
GetComponent<YourComponentType>().yourVariable = the value;
Thanks very much for the fast reply. Can you clarify what you mean by YourComponentType? The component is just a script or monobehaviour, so what would I use? Thanks again for the reply
When you declare a $$anonymous$$onoBehaviour, it has a type
public class PlayerHealth : $$anonymous$$onoBehaviour // PlayerHealth is the type here
{
public int health = 100;
}
So you can do
GetComponent<PlayerHealth>().health -= 1;
If your intent is to access a given property on any $$anonymous$$onoBehaviour or Script, you would have to use Reflection.
Something in the likes :
using System.Reflection;
$$anonymous$$onoBehaviour[] all$$anonymous$$onoBehaviours = GetComponents<$$anonymous$$onoBehaviour>();
foreach ($$anonymous$$onoBehaviour mono in all$$anonymous$$onoBehaviours)
{
PropertyInfo[] allProperties = mono.GetType().GetProperties (BindingFlags.Instance | BindingFlags.Public);
for (int i = 0; i < allProperties.Length; i++)
{
allProperties[i] // query the property to see if it's the right type/name, etc..
}
}
This comes at a cost and isn't supported on all platforms. I find it mostly useful for Editor tools.
if (combine$$anonymous$$ag1.name == "pistol$$anonymous$$agItem") {
//create variable that allows me to access the script in the if statement below
haveComponent = true;
}
if (haveComponent == true) {
if (combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem>().currentCapacity != combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem>().maxCapacity) {
maxToReload = combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().maxCapacity - combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity;
if (combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem>().currentCapacity > maxToReload && processed == false) {
combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity = combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity + maxToReload;
combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity = combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity - maxToReload;
processed = true;
}
if (combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem>().currentCapacity == maxToReload && processed == false) {
combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity = combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity + maxToReload;
processed = true;
Destroy (combine$$anonymous$$ag2);
}
if (combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem>().currentCapacity < maxToReload && processed == false) {
combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity = combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity + combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity;
processed = true;
Destroy (combine$$anonymous$$ag2);
}
}
}
How would I alter this code to make it work? The reason I do this is because it would be much more optimised to just assign the script in a variable then to write the whole code out over and over and over accessing different scripts.
I sent a reply yesterday but I don't think it went through (well i dont see it soooooo). The following is my script: if (combine$$anonymous$$ag1.name == "pistol$$anonymous$$agItem") { //create variable that allows me to access the script in the if statement below haveComponent = true; } if (haveComponent == true) { if (combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem>().currentCapacity != combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem>().maxCapacity) { maxToReload = combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().maxCapacity - combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity; if (combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem>().currentCapacity > maxToReload && processed == false) { combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity = combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity + maxToReload; combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity = combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity - maxToReload; processed = true; } if (combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem>().currentCapacity == maxToReload && processed == false) { combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity = combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity + maxToReload; processed = true; Destroy (combine$$anonymous$$ag2); } if (combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem>().currentCapacity < maxToReload && processed == false) { combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity = combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity + combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity; processed = true; Destroy (combine$$anonymous$$ag2); } } }
as you can see i use .getcomponent