Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • 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
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

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 · Feb 09, 2019 at 06:08 PM · scripting problemgameobjectvariabletriggersefficiency

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 · Feb 09, 2019 at 06:56 PM 1
Share

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)
     }
 }
avatar image KenanTheFab RobAnthem · Feb 09, 2019 at 06:57 PM 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 · Feb 09, 2019 at 07:01 PM 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 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.
 {
 }
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

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 · Feb 11, 2019 at 09:17 AM 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.

Follow this Question

Answers Answers and Comments

211 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

Related Questions

How to detect if two objects become a square ? 1 Answer

Cannot destroy Component while GameObject is being activated or deactivated 2 Answers

Raycast object tag check? 3 Answers

SOLVED - Accessing a C# script value from another GameObject's JavaScript 2 Answers

(PLAYMAKER) How to synchronize global variables to another visual scripting? (like Uscript or Behavior Machine Pro) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges