- Home /
Efficiently changing other object's variables via triggers?
Hello! I've been googling a lot lately because I am hoping for a more efficient/easy way to do the following:
I have 2 scripts, one is for triggers, another is on a gameobject with a value i wanna change (Lets call them TG and GV respectively)
I want it so that I can define a GV's variable (bool, int, float.) to change in the inspector of TG, and not have to manually type out what gameobject to find, the script name, etc, and I was hoping I could define both in the inspector (like one can do with gameobjects and various other elements for ease of use) and then also get the variable of said object's script and be able to change it.
I've experimented with Getcomponent(stringInput) but I don't seem to be able to do the variable part.
Is there an easier way for this or will I just have to do things very manually?
Well there is actually this nifty function called Component.Send$$anonymous$$essageUpwards
that sends a function to a script without needing that script to have the required function. So it will ignore it if it doesn't apply. So, what you can do is on your GV object, have something like this.
public class ObjectValues : $$anonymous$$onoBehaviour
{
public float floatValue;
public string stringValue;
public int integerValue;
public void SetValue(object value)
{
if (value is float)
{
floatValue = (float)value;
}
else if (value is string)
{
stringValue = value.ToString();
}
else if (value is int)
{
integerValue = (int)value;
}
}
}
And for your script that handles the trigger collision...
public class TriggerObject : $$anonymous$$onoBehaviour
{
public string functionString = "SetValue";
public float floatToSet = 2.0f;
void OnTriggerEnter(Collider other)
{
other.transform.Send$$anonymous$$essageUpwards(functionString, floatToSet)
}
}
Ack! thank you! Will test it asap!!! Though how will this fare with multiple objects having the same script? Or would "other" be the defined gameobject and I am being silly? haha
Other is only the collider on the gameobject that was triggered in the trigger event, but NOT the gameobject that contains the trigger script. The other collider is the default inco$$anonymous$$g parameter of the GameObject function OnTriggerEnter. You can name it whatever you want like.
void OnTriggerEnter(Collider colliderThatWasHit)
{
colliderThatWasHit.transform.Send$$anonymous$$essageUpwards("function", 1);
}
Think of these default functions as Override functions without requiring the override keyword. There are a lot of them and each one has its own inco$$anonymous$$g parameters, or none at all. Some examples of these functions are...
void Update() // runs every frame
{
}
void FixedUpdate() // runs every physics frame
{
}
void LateUpdate() // runs after Update. Like a secondary update, in case other objects update was dependent on other objects. $$anonymous$$ost common reason for this function to be used is Camera movement. That way the camera is always at its final position.
{
}
void OnEnable() // occurs when an object is Enabled including when it is first created or a scene is loaded.
{
}
void Awake() // occurs once, the very first function to run on every script when it is first initialized on game loading/starting or instantiating.
{
}
void Start() // similar to Awake but runs afterward.
{
}
void On$$anonymous$$ouseDown() // only applies to objects with colliders, occurs when a mouse button is clicked on the object.
{
}
void OnCollisionEnter(Collision col) // occurs when this object collides with another, and gives Collision data about the event.
{
}
Answer by Chimer0s · Feb 11, 2019 at 12:58 AM
Are both of these objects present in the scene at all times? If so you can create a public reference to the GV script in your TG script and just drag the GV script into it the inspector. You can use your own scripts the same way you would any other component when declaring it in your class header. i.e. Public GV gvReference;
and then just drag the game object with GV attached into the inspector like you would with any other variable.
I was hoping for something somewhat flexible! So I wouldn't have to create tons of scripts for every trigger with different gameobjects and gameobject values to set! Otherwise I would just do that.
What I was hoping for is something more like...
OnTriggerEnter (or something else, but lets focus on triggers)
{
DefinedGameObject.DefinedScript.DefinedVariable = DefinedChange.
}
With all four being changeable, so I can easily get a game object's script, then get one of the script's variables, and then change it respectively.
Your answer
