Search Unity

Changing Move Speed Multiplier from other script

Discussion in 'Editor & General Support' started by bod, Apr 23, 2015.

  1. bod

    bod

    Joined:
    Jul 31, 2014
    Posts:
    17
    I am trying to slow the speed of the player when it collides with a trigger. I'm using the ThridPersonControler in Unity 5. The ThirdPersonCharacter.cs script has "[SerializeField] float m_MoveSpeedMultiplier = 1f;" how do I change this value from another script?
     
  2. StarGameStudios

    StarGameStudios

    Joined:
    Feb 7, 2015
    Posts:
    3
    Hi, I have the same problem, but I need when button down my player run fast. Do you have find soluction to this problema? If yes, please share with me.
    I know if without [SerializeField] and put public variable is possible, but with [SerializeField] I dont find a soluction.​
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    SerializeField only means that the field is serialized even if it's not public.

    The easiest way would either be to set the field as public:

    Code (csharp):
    1. public float m_MoveSpeedMultiplier
    Or to create a method that sets the speed:

    Code (csharp):
    1. public void SetSpeedMultiplier(float newSpeedMultiplier) {
    2.     m_MoveSpeedMultiplier = newSpeedMultiplier;
    3. }
     
    StarGameStudios likes this.
  4. StarGameStudios

    StarGameStudios

    Joined:
    Feb 7, 2015
    Posts:
    3
    Thank you for your answer. But what is the function of the [SerializeField]. If you take out, what could happen?
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    It makes the data you put there be serialized (saved to disk) by Unity. Which means that you can change the values in the inspector, and they'll stay set to the values you put in.

    For public fields, the data is always saved if it is of a type Unity can serialize. You can read the exact details here, but float and other primitive types can always be serialized. This means that putting [SerializeField] over a public field is redundant.

    If you're going to use the setter, you'll want to keep the field private, and keep the [SerializeField] tag around, as otherwise the value won't be possible to set from the inspector, and will be returned to the default value (0) when you press play.
     
  6. bod

    bod

    Joined:
    Jul 31, 2014
    Posts:
    17
    Thanks Baste I'll give that a try, and thanks for the explanation of the [SerializeField] I had not encountered it before and was not sure how it was appearing in the editor. :)
     
  7. StarGameStudios

    StarGameStudios

    Joined:
    Feb 7, 2015
    Posts:
    3
    Thanks Baste. Thank you very much for the axplanation of the [SerializeField]. In my case, I'm out [SerializeField] and work fine. Thanks.
     
  8. bod

    bod

    Joined:
    Jul 31, 2014
    Posts:
    17
    Once again Thanks Baste, your solution worked 100% :)