Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 TheGeneralBenLee · Apr 14, 2021 at 10:30 AM · scripting problembeginnersendmessagetwosend-variable

How do I send a variable (after calculation) as the final variable to another game object

I don't think i worded my question correctly, but I have a player script monitoring colliders using Physics.OverlapSphere and this in turn does send a message and the function on the other script damages the object. What i am trying to do is have the player hold a universal value, like damage = 2, but i want to do a calculation in a method in the player script, so finalDamage = damage * 2 for example. I want to know if there is a way for me to, after the value is calculated, physically send that unique value to what was hit, rather than having to have each object hold its own value to be damaged by.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by UnityM0nk3y · Apr 14, 2021 at 10:42 AM

Well indeed there are many ways to "physically send values".

I would recommend you just reference the value directly, here let me explain with some code:

Suppose this is "script01" and you have the "damage" as a public variable:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class script02 : MonoBehaviour
 {
     public float damage;
 }




Now to get that "damage" from another script, you simply need to do this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class testScript : MonoBehaviour
 {
     [SerializeField] private script02 s02; //Don't forget to assign in inspector
 
     private float damage;
 
     void retrieveDamage()
     {
         damage = s02.damage;
         //this will set our damage on this code, to the "damage" of that code.
     }
 }


Comment
Add comment · Show 7 · 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 TheGeneralBenLee · Apr 14, 2021 at 12:17 PM 0
Share

can i assign the script from code? i seem to remember being able to do stuff like that but cant remember the syntax.

avatar image HellsHand TheGeneralBenLee · Apr 14, 2021 at 12:45 PM 0
Share

To add a script during Runtime you could use gameObject.AddComponent<Your Script>();.

avatar image unity_0D01B6DF5482F8E52C3D HellsHand · Jun 02, 2021 at 06:35 PM 0
Share

I have a question, @HellsHand, how can you (on the JS side after building in WebGL) go past this error: SendMessage: object Object1 does not have receiver for function moveObject

Please check out this question: https://answers.unity.com/questions/1838034/unity-webgl-unityinstancesendmessage-error-sendmes.html

avatar image HellsHand · Apr 14, 2021 at 12:43 PM 0
Share

As this answer is not wrong I will just leave this as a comment. It is always better to reference a public function than a public variable when making a game, it limits the end user from cheating/hacking your game. A slight variation of the above code:

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class script02 : MonoBehaviour
  {
      float damage = 10f;  //public works for checking in inspector but in the end this should be initialized with a value.
 
      public float GetDamage()
      {
          return damage * 2;
      }
  }

The second script would look like this:

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class testScript : MonoBehaviour
  {
      [SerializeField] private script02 s02; //Don't forget to assign in inspector
  
      private float damage;
  
      void retrieveDamage()
      {
          damage = s02.GetDamage();
          //this will set our damage on this code, to the "damage" of that code.
      }
  }
avatar image TheGeneralBenLee · Apr 14, 2021 at 01:01 PM 0
Share

thank you both! I love the unity community just so helpful! :)

avatar image unity_0D01B6DF5482F8E52C3D TheGeneralBenLee · Jun 02, 2021 at 06:36 PM 0
Share

I have a question, @TheGeneralBenLee, how can you (on the JS side after building in WebGL) go past this error: SendMessage: object Object1 does not have receiver for function moveObject

Please check out this question: https://answers.unity.com/questions/1838034/unity-webgl-unityinstancesendmessage-error-sendmes.html

avatar image unity_0D01B6DF5482F8E52C3D · Jun 02, 2021 at 06:35 PM 0
Share

I have a question, @UnityM0nk3y, how can you (on the JS side after building in WebGL) go past this error: SendMessage: object Object1 does not have receiver for function moveObject

Please check out this question: https://answers.unity.com/questions/1838034/unity-webgl-unityinstancesendmessage-error-sendmes.html

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

227 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

groundCheck in small beginner project not working properly 0 Answers

Dynamic Buttons, SciptableObjects and Lists 1 Answer

main menu not loading level 1 please help beginner here 0 Answers

Water collider in script 0 Answers

First script plays animation but second script stops if from playing said animation 1 Answer


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