Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Unity 2018.3 is now released.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

changing public float from another script

Discussion in 'Scripting' started by gohchersee, Feb 11, 2019.

  1. gohchersee

    gohchersee

    Joined:
    Feb 11, 2019
    Posts:
    1
    Hi, I am relatively new to c# and unity. I have two script


    Code (CSharp):
    1. public class DialogueManager : MonoBehaviour {
    2.  
    3.     public Animator animator;
    4.  
    5. public void StartDialogue(Dialogue dialogue)
    6.     {
    7.         animator.SetBool ("IsOpen", true);
    8.         nameText.text = dialogue.name;
    9.         sentences.Clear ();
    10.         foreach (string sentence in dialogue.sentences)
    11.         {
    12.             sentences.Enqueue (sentence);
    13.         }
    14.         DisplayNextSentence();
    15.     }
    16.  
    17.     void EndDialogue(){
    18.         animator.SetBool ("IsOpen", false);
    19.     }
    and

    Code (CSharp):
    1. public class Note : MonoBehaviour {
    2.     Rigidbody2D rb;
    3.     public float speed;
    4.     void Awake(){
    5.         rb = GetComponent<Rigidbody2D> ();
    6.     }
    7.  
    8.     void Start () {
    9.         rb.velocity = new Vector2 (0, -speed);
    10.     }
    I am trying to access public float speed from Note script in DialogueManager script.
    The end goal is when
    animator.SetBool ("IsOpen", true);
    public float speed = 0;

    and when

    animator.SetBool ("IsOpen", false);
    public float speed will return back to its oringnal value.
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    2,867
    Code (CSharp):
    1. [RequireComponent(typeof(Note))]
    2. public class DialogueManager : MonoBehaviour {
    3.     public Animator animator;
    4.     public Note noteScript;
    5.     private float originalSpeed = 0f;
    6.  
    7. void Awake() {
    8.    noteScript = GetComponent<Note>();
    9. }
    10. public void StartDialogue(Dialogue dialogue)
    11.     {
    12.       // example
    13.       originalSpeed = noteScript.speed;
    14.       noteScript.speed = 0f;
    15. // [...]
    16.  
    17.    void EndDialogue(){
    18.         animator.SetBool ("IsOpen", false);
    19.         noteScript.speed = oiriginalSpeed;
    20.     }
    21.  
    22.  
    Alternatively you can create two methods in the Note script like this:
    Code (CSharp):
    1. private float speed = 0f; // replace the original
    2. float originalSpeed = 0f;
    3.  
    4. public void SetTempSpeed(float newSpeed)
    5. {
    6.     originalSpeed = speed;
    7.     speed = newSpeed;
    8. }
    9.  
    10. public void RevertSpeed()
    11. {
    12.      speed = originalSpeed;
    13. }
    and then just call them from the other script:
    Code (CSharp):
    1. [RequireComponent(typeof(Note))]
    2. public class DialogueManager : MonoBehaviour {
    3.     public Animator animator;
    4.     public Note noteScript;
    5.  
    6. void Awake() {
    7.    noteScript = GetComponent<Note>();
    8. }
    9. public void StartDialogue(Dialogue dialogue)
    10.     {
    11.       // example
    12.       noteScript.SetTempSpeed(0f);
    13.  
    14. // [...]
    15.  
    16.     void EndDialogue(){
    17.         animator.SetBool ("IsOpen", false);
    18.         noteScript.RevertSpeed();
    19.     }
    20.  
    21.  
     
    gohchersee likes this.