• Unity
  • Services
  • Made with Unity
  • Learn
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by KenanTheFab · 6 days ago · gameobjectscripting problemvariabletriggersefficiency

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?

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RobAnthem · 6 days ago 1
Share

Well there is actually this nifty function called Component.SendMessageUpwards 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 : MonoBehaviour
 {
     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 : MonoBehaviour
 {
     public string functionString = "SetValue";
     public float floatToSet = 2.0f;
     void OnTriggerEnter(Collider other)
     {
         other.transform.SendMessageUpwards(functionString, floatToSet)
     }
 }
avatar image KenanTheFab RobAnthem · 6 days ago 0
Share

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

avatar image RobAnthem KenanTheFab · 6 days ago 0
Share

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 incoming parameter of the GameObject function OnTriggerEnter. You can name it whatever you want like.

 void OnTriggerEnter(Collider colliderThatWasHit)
 {
     colliderThatWasHit.transform.SendMessageUpwards("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 incoming 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. Most 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 OnMouseDown() // 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.
 {
 }
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Chimer0s · 5 days ago

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KenanTheFab · 4 days ago 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

218 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Duplicate object and shooting problem (SOLVED) 1 Answer

How to set/get/change variables for newly spawned gameObjects? 1 Answer

Unable to assign every Instantiated gameobject under a variable ? 1 Answer

How to add multiple scripts to a script? (C#) 2 Answers

How can I use GameObject.Find with a string and a variable ? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges