Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

New in Unity (and C#). How to get variable from another script?

Discussion in 'Scripting' started by dannythareboss, Sep 12, 2016.

  1. dannythareboss

    dannythareboss

    Joined:
    Sep 12, 2016
    Posts:
    4
    I'm trying to get a variable from another script then if that variable is bigger than X, the variable will increase with 10.

    I have a script called Bird, this script has 3 variables (
    public float speed = 2;
    public float force = 300;
    public float counter = 0; )

    Speed is the actual speed of the player, and counter will increase everytime players pressed Space key.

    I have tried to make another script myself but I failed so bad...
    I need this kind of script to call more variables from another scripts, but for now I tried to make this.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CounterCall : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.  
    9.         GameObject.Find ("Bird").GetComponent<Bird> ();
    10.    
    11.         if (Bird.counter == 5) {
    12.  
    13.             speed =+ 10;
    14.  
    15.         }
    16.  
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.    
    22.  
    23.  
    24.     }
    25. }
    26.  

    I wanted to make the player (Bird script) to increase the speed with 10 when "counter" variable is 5.
    But seems that I don't know how to use conditionals in Unity or using variable from another script..

    Can someone help me please?

    (and yes , I know that i should learn C# better, but that's why I'm also asking others for help)
     
  2. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    you could make it easier by adding a public gameobject, dragging the "bird" gameobject into it, then getting the variables like so,

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class CouncerCallt : MonoBehaviour
    5. {
    6.  
    7.     public GameObject bird; // put the gameobject which has the bird script on this.
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.  
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         if (bird.GetComponent<Bird>().counter == 5)
    18.         {
    19.             bird.GetComponent<Bird>().speed = 10.0f;
    20.         }
    21.     }
    22. }
    23.  
     
    Ryiah likes this.
  3. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,886
    Or just do "public Bird birdScript" and drag the gameobject in. It will automatically get the component.
     
    Ryiah likes this.
  4. dannythareboss

    dannythareboss

    Joined:
    Sep 12, 2016
    Posts:
    4
    Okay, I can do that. But is the rest of the code okay? I don't think it will work

    (Edit: The script works, but the speed in game won't increase. (the speed in Inspector is increased though...)
     
    Last edited: Sep 12, 2016
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you've not shared the code which uses speed... so *shrugs*
     
  6. dannythareboss

    dannythareboss

    Joined:
    Sep 12, 2016
    Posts:
    4
    I am trying to make a script visible when counter is 50, but seems like I can't get it...

    TheScript is like this:

    Code (CSharp):
    1.     public Bird bird;
    2.     public VisualEffect visualEffect;
    3.  
    4.     void Start()
    5.     {
    6.  
    7.     }
    8.  
    9.     // Update is called once per frame
    10.     void Update()
    11.     {
    12.         if (bird.GetComponent<Bird>().counter == 50)
    13.         {
    14.             visualEffect.GetComponent<VisualEffect>().enabled = true;
    15.         }
    16.     }
    The visual effect script is in another folder that the rest of scripts, but it's still in Assets folder.
    Also, I can't get it where to put the script name, or if i dragNdrop it, it still can't get accesed.
    (Bird script works though)
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    You need to actually have the VisualEffect script on a GameObject in the scene, and enable that instance of the script.

    Right now, you're trying to make an asset that's sitting in a folder turn on. That's not going to do very much!
     
  8. dutchkiller2000

    dutchkiller2000

    Joined:
    May 13, 2016
    Posts:
    121
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class test : MonoBehaviour {
    6.     scriptname scriptname;
    7.     // Use this for initialization
    8.     void Start () {
    9.         scriptname = GameObject.Find("item with script atacht").GetComponent<scriptname>();
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.         scriptname.tosomething;
    15.     }
    16. }