- Home /
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.
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.
}
}
can i assign the script from code? i seem to remember being able to do stuff like that but cant remember the syntax.
To add a script during Runtime you could use gameObject.AddComponent<Your Script>();
.
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
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.
}
}
thank you both! I love the unity community just so helpful! :)
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
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
Follow this Question
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