- Home /
how to access a float from one script in other script in different objects
hi there, i have been digging for a while and tried multiple suggestions from similar questions but nothing is working, as you will understand i am very new to this and probably my problem is that i dont know exacly how to ask my issue. so here it goes:
i have a bunch of floats in various gameobjects and i want to have all that variable floats in one single component. so i created an empty object and named Controls and made a script to call for the floats.
i then call for a script from other object and tried to access the float from that script in this Controls script.
bellow the script i wanna call in Control:
public class PlayerMovement : MonoBehaviour
public float Jump;
public float Turn;
void Update()
{
if (Input.GetKey("space"))
{
Rb.AddRelativeForce(0, Jump * Time.deltaTime, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("e"))
{
transform.Rotate(0, Turn * Time.deltaTime, 0, (Space)ForceMode.VelocityChange);
}
if (Input.GetKey("q"))
{
transform.Rotate(0, -Turn * Time.deltaTime, 0, (Space)ForceMode.VelocityChange);
}
}
Bellow the Universal Control Script
public class Controls : MonoBehaviour
{
public GameObject Player;
private PlayerMovement PM;
public float jump = 1000f;
public float turn = 200f;
public void Call()
{
PM = Player.GetComponent<PlayerMovement>();
}
public void Control()
{
PM.Jump = jump;
PM.Turn = turn;
}
} Clearly its not working as intended, but the idea would be to call multiple scripts and use all the variables as floats in this empty game object.
this would be only for development so i don't care if its heavy its just for development faze then the variables would go to a settings menu or be fixed all together.
Thanks in advance i understand this might be straight forward but i didnt find the solution.
Why is the update method not in a class in the first script?
AG45 its in the Player$$anonymous$$ovement class i just forget to add the { after on this question, i just placed what i thought was relevant didnt post the whole code
i dont know what you mean .sorry i am new as i mention.
Answer by ShadyProductions · Aug 26, 2020 at 12:54 PM
Why not just make it a constants:
public static class Constants
{
public static class PlayerControls
{
public const float Jump = 1000f;
public const float Turn = 200f;
}
}
Then in your PlayerMovement class:
public class PlayerMovement : MonoBehaviour
{
public float Jump = Constants.PlayerControls.Jump;
public float Turn = Constants.PlayerControls.Turn;
}
You need to be careful, because modifying the jump/turn value of PlayerMovement in inspector, will overwrite constant value. Better to then make the value either private, or use [HideInInspector] attribute to hide it.
the 'Constants' does not exist in the current context
am i missing something? i just changed what i have with your script
only using UnityEngine in this Player$$anonymous$$ovement
$$anonymous$$ake sure its under the same namespace
how do i verify that? because i am using visual studio with unity and i dont have namespace, i have been seeing C# basics and i now know what is namespace but the scripts that unity "provides" do not use namespace at least obviously like in the c# guides show before class . the top of my scripts have the using UnityEngine and a class. i already mention in the beginning i started learning very recently i am sorry if this seems obvious for you. for what i am trying i need them to be two different scripts from two different GameObjects but one will be the hub for multiple float controls.
Thanks ShadyProductions
using UnityEngine;
using UniBus;
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour
{
public Rigidbody Rb;
[SerializeField]
private Controls _controls;
private float Jump;
private float Turn;
void Update()
{
Jump = _controls.Jump;
Turn = _controls.Turn;
using UnityEngine;
namespace UniBus
{
public class Controls : $$anonymous$$onoBehaviour
{
public float Force;
public float Jump;
public float Turn;
}
}
Answer by Llama_w_2Ls · Aug 26, 2020 at 01:41 PM
You can also find an object in your scene and get the script component of that by using GameObject.Find("...");
where the '...' is the name of the object in the scene
hi thanks, ok i actually have tried that but how then to use it as i intend? i call the object and use the rest of my code as above?
For example, if your main script is on your Game$$anonymous$$anager gameObject and you want to get a float from the Game$$anonymous$$anager and put it into your secondary script, you could go:
public float SecondaryFloat;
void Start()
{
$$anonymous$$ainScript _mainScript = GameObject.Find("Game$$anonymous$$anager").GetComponent<$$anonymous$$ainScript>();
SecondaryFloat = _mainScript.PrimaryFloat;
}
Preferably you wouldn't be using GameObject.Find at all, because it is bad for performance. It is much better to just assign the Game$$anonymous$$anager gameobject in the inspector in that case.
Answer by GamerLordMat · Aug 27, 2020 at 08:36 AM
I don't see why your script shoulnd't work. You just have to call Call() in Controls start function.
well i took that script from a tutorial from unity themselves i just didnt know that i needed to call(), but now i got it working with [SerializeField] thanks anyway
Your answer
Follow this Question
Related Questions
Object reference not set to an instance of an object 3 Answers
I have a little problem getting a component from another script 1 Answer
Does not contain definition 1 Answer
How to assign a Component to a Public Variable if there are multiple of the same kind? 1 Answer
How to Access Components in Children of GameObject? 1 Answer