Search Unity

my scripts cant read eatchother

Discussion in 'Scripting' started by Slinta, Sep 19, 2014.

  1. Slinta

    Slinta

    Joined:
    Sep 19, 2014
    Posts:
    8
    So i am trying to make a game, its kinda like flappy bird, but waay different. I decided that i want to make the score Time lasted.
    so i wrote this:

    static public float time;
    void Update ()
    {
    time = Time.realtimeSinceStartup;
    guiText.text = "lasted:" + time;
    }
    i put this on GUIText object
    Now i have the second objetc, the player:

    public Vector3 velocity;
    public Vector3 gravity;
    public bool dead = false;
    float deathcd;

    void OnTriggerEnter2D(Collider2D collider)
    {
    if (collider.name == "krapnik")
    {
    dead = true;
    }
    if (collider.name == "wall")
    {
    dead = true;
    }
    }
    void Start (){
    deathcd = 20;
    }
    void Update ()
    {

    if (dead == true)
    {
    Application.LoadLevel( Application.loadedLevel );
    Here, i want to somehow reset the time to 0, but i cant do:
    time = 0;
    becouse unity writes that it does not know what time is
    }


    transform.position += gravity * Time.deltaTime;
    if(Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0) )
    {
    transform.position += velocity * Time.deltaTime;
    }
    }

    }
     
  2. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    if time is in a different script, then you need a reference to that script.
    e.g.

    public TimeScript timeScriptReference; //add this with your variables at the top of the second script.
    Then assign it in the inspector, by dragging the first script into the variable slot.
    Then in your second script, you can do:
    timeScriptReference.time = 0;

    Hope this helps! :)
     
  3. Slinta

    Slinta

    Joined:
    Sep 19, 2014
    Posts:
    8
    well i referenced it other way, but now i have another problem, the game freezes itself and just doesnt run anymore,
    i need a look at this new code :

    public class UPDOWN : MonoBehaviour {
    static public float time;
    public Vector3 velocity;
    public Vector3 gravity;
    static public bool dead = false;



    void OnTriggerEnter2D(Collider2D collider)
    {
    if (collider.name == "krapnik")
    {
    dead = true;
    }
    if (collider.name == "wall")
    {
    dead = true;
    }
    }

    void Start()
    {

    }
    public void Update ()
    {

    if (dead == true)
    {
    Application.LoadLevel( Application.loadedLevel );
    }


    transform.position += gravity * Time.deltaTime;
    if(Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0) )
    {
    transform.position += velocity * Time.deltaTime;
    }
    }

    }

    public class ScoreCounter : MonoBehaviour {

    float timecurrent = 0;
    public Vector3 antigravity;
    static public float time;
    static float currentruntime = 0;
    public static void clearscore()

    {

    }
    void Start () {

    }

    public void Update () {
    time = Time.realtimeSinceStartup;
    GameObject Cube = GameObject.Find("Cube");
    UPDOWN kostka = Cube.GetComponent<UPDOWN>();

    if (UPDOWN.dead = true)
    {
    currentruntime = time;
    }
    timecurrent = time - currentruntime;
    guiText.text = "lasted:" + timecurrent;
    transform.position += antigravity * Time.deltaTime;
    }
    }
     
  4. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    well the only thing I can see that's bad with your current code is that you're doing both a Find and GetComponent inside your update loop, you should move those to start and store the results in variables, and use the variables to access these things. Hope this helps! :3