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

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.

using UnityEngine;
using System.Collections;

public class CounterCall : MonoBehaviour {

    // Use this for initialization
    void Start () {

        GameObject.Find ("Bird").GetComponent<Bird> ();
   
        if (Bird.counter == 5) {

            speed =+ 10;

        }

    }
   
    // Update is called once per frame
    void Update () {
   


    }
}

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)

you could make it easier by adding a public gameobject, dragging the “bird” gameobject into it, then getting the variables like so,

using UnityEngine;

public class CouncerCallt : MonoBehaviour
{

    public GameObject bird; // put the gameobject which has the bird script on this.
    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (bird.GetComponent<Bird>().counter == 5)
        {
            bird.GetComponent<Bird>().speed = 10.0f;
        }
    }
}
1 Like

Or just do “public Bird birdScript” and drag the gameobject in. It will automatically get the component.

1 Like

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…)

you’ve not shared the code which uses speed… so shrugs

I am trying to make a script visible when counter is 50, but seems like I can’t get it…

TheScript is like this:

    public Bird bird;
    public VisualEffect visualEffect;

    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (bird.GetComponent<Bird>().counter == 50)
        {
            visualEffect.GetComponent<VisualEffect>().enabled = true;
        }
    }

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)

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!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour {
    scriptname scriptname;
    // Use this for initialization
    void Start () {
        scriptname = GameObject.Find("item with script atacht").GetComponent<scriptname>();
    }
  
    // Update is called once per frame
    void Update () {
        scriptname.tosomething;
    }
}