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 /
  • Help Room /
avatar image
0
Question by sadowlight123 · Jun 22, 2017 at 08:19 PM · scripting problemarrayvariableaccessing scripts

Access variable of same script with many instances

Hello guys , I have a script called movement. This script contains a variable called Height. The Movement script is placed on two gameObjects in the scene. I have another script called Manager. At some point the Manager script has to access the Height from the two Movement scripts and change it. What i did is the following in the manager script public Movement [] Movement; And dragged and dropped the two Objects that has the Movement scripts. I think this way is not good and would like to find a better more efficient alternative . I am not sure that a static variable will help since the Height needs to be 0 each time the player restarts the level. Any suggestions? Thanks a lot for the help!

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
0

Answer by andrew-lukasik · Jun 22, 2017 at 08:40 PM

You can try events/method groups (or whatever it's actually called). Idea here is to have a public static field to which you can subscribe/unsubscribe methods of it's matching type (here - subscribing methods must accept 1 float argument). Then you can access and execute this event/method group what will execute all subscribed methods so far (so on all instances here)

 public class Manager : MonoBehaviour
 {
 
     void Start ()
     {
         //reset height:
         if( Movement.changeHeight != null )
         {
             Movement.changeHeight( 0f );
         }
     }
 
     void LaterOn ()
     {
         //set new height:
         if( Movement.changeHeight != null )
         {
             Movement.changeHeight( 1f );
         }
     }
 }
 
 public class Movement : MonoBehaviour
 {
     float _height;

     //public static event/method group to which methods can be subscribed:
     public static System.Action<float> changeHeight;
 
     void OnEnable ()
     {
         //subscribe to event when object is activated:
         changeHeight += OnHeightChanging;
     }
 
     void OnDisable ()
     {
         //unsubscribe from event when object is deactivated:
         changeHeight -= OnHeightChanging;
     }

     //method that got subscribed (handling _height change):
     void OnHeightChanging ( float newHeight )
     {
         this._height = newHeight;
     }
 }




OR you can handle this with list of instances. This is imo simpler solution, you can stick to this for now and try events another time.

Idea here is to make Movements class maintain static and always up to date list of existing instances of it's type. Then any class can access those whenever needed:

 /// <summary>
 /// Manager class
 /// </summary>
 public class Manager : MonoBehaviour
 {
     #region monobehaviour_methods
 
     void Start ()
     {
         //reset height:
         foreach( var item in Movement.instances )
         {
             item.SetHeight( 0f );
         }
     }
 
     #endregion
     #region public_methods
 
     /// <summary>
     /// Some method you execute later on
     /// </summary>
     void LaterOn ()
     {
         //set new height:
         foreach( var item in Movement.instances )
         {
             item.SetHeight( 1f );
         }
     }
 
     #endregion
 }


 using System.Collections.Generic;
 
 /// <summary>
 /// Movement class
 /// </summary>
 public class Movement : MonoBehaviour
 {
     #region fields
 
     /// <summary>
     /// This list will store all existing instances of type Movement
     /// </summary>
     public static List<Movement> instances = new List<Movement>();
 
     /// <summary>
     /// height value
     /// </summary>
     float _height;
 
     #endregion
     #region monobehaviour_methods
 
     void OnEnable ()
     {
         //add this to list of instances:
         instances.Add( this );
     }
 
     void OnDisable ()
     {
         //remove this from list of instances:
         instances.Remove( this );
     }
 
     #endregion
     #region public_methods
 
     /// <summary>
     /// Sets the height.
     /// </summary>
     public void SetHeight ( float newHeight )
     {
         this._height = newHeight;
     }
 
     #endregion
 }









Comment
Add comment · Show 2 · 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 sadowlight123 · Jun 23, 2017 at 07:43 AM 0
Share

sorry , I didn't understand . Could you please include a bit more detailed explanation ? Thank you in advance

avatar image andrew-lukasik sadowlight123 · Jun 23, 2017 at 06:02 PM 0
Share

Ok, updated. I added few more explanations and 1 whole different approach (static list of instances)

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

109 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

Related Questions

Advanced C# problem: Timings incorrect in script, assigning a variable that is not updated yet in an array. 0 Answers

Array index out of range. But it doesn't appear to be. 2 Answers

Custom inspector. Pick one bool from a list. 1 Answer

Assign values to array elements 1 Answer

Get value from another script 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