Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Freakhealer · Aug 26, 2020 at 12:21 PM · gameobjectgetcomponentfloatpublic variableshare

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.

Comment
Add comment · Show 4
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 AG45_ · Aug 26, 2020 at 01:00 PM 0
Share

Why is the update method not in a class in the first script?

avatar image ShadyProductions AG45_ · Aug 26, 2020 at 01:01 PM 0
Share

pseudo code :)

avatar image Freakhealer AG45_ · Aug 26, 2020 at 10:15 PM 0
Share

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

avatar image Freakhealer · Aug 26, 2020 at 01:11 PM 0
Share

i dont know what you mean .sorry i am new as i mention.

3 Replies

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

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.

Comment
Add comment · Show 8 · 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 Freakhealer · Aug 26, 2020 at 01:11 PM 0
Share

will try that, thanks

avatar image Freakhealer · Aug 26, 2020 at 01:20 PM 0
Share

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

avatar image ShadyProductions Freakhealer · Aug 26, 2020 at 01:33 PM 0
Share

$$anonymous$$ake sure its under the same namespace

avatar image Freakhealer ShadyProductions · Aug 26, 2020 at 09:36 PM 0
Share

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.

Show more comments
avatar image Freakhealer · Aug 27, 2020 at 11:41 AM 0
Share

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;
       }
   }
avatar image
0

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

Comment
Add comment · Show 6 · 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 Freakhealer · Aug 26, 2020 at 09:38 PM 0
Share

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?

avatar image Llama_w_2Ls Freakhealer · Aug 27, 2020 at 08:26 AM 0
Share

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;
 }
 
avatar image ShadyProductions Llama_w_2Ls · Aug 27, 2020 at 08:28 AM 1
Share

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.

Show more comments
avatar image Freakhealer · Aug 27, 2020 at 11:36 AM 0
Share

Thanks guys its working now, with [serializefield]

avatar image
0

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.

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 Freakhealer · Aug 27, 2020 at 11:35 AM 0
Share

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

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

210 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

Related Questions

I have a little problem getting a component from another script 1 Answer

Does not contain definition 1 Answer

Object reference not set to an instance of an object 3 Answers

Assign object in hierarchy as public variable in prefab 1 Answer

Getting errors when referencing a variable. 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