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

Changing vales in a deferent class

Discussion in 'Editor & General Support' started by NOIG, Sep 25, 2014.

  1. NOIG

    NOIG

    Joined:
    Jul 15, 2014
    Posts:
    32
    Ok i think i know how to do this, but i would like to see how other people would do it,
    Ok so in my game you can but items, wich is in items.cs, and some of them change the speed of the character.
    So i need it to change the float in Movement.cs from like 8 to 8.5 from items.cs, so how would i do that
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You need to get a reference to the script, which allows you to access the public methods and attributes of that instance.
    If you use static member variables, you can also access them via the class directly.
     
  3. NOIG

    NOIG

    Joined:
    Jul 15, 2014
    Posts:
    32
    Yo thats what i was going to do, but i was wandering is the a better more optimal way of doing it
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Why do you think it's not optimal? It's a quite nice and easy way.
     
  5. NOIG

    NOIG

    Joined:
    Jul 15, 2014
    Posts:
    32
    Because i made a game before, that used that and the hole game was around picking things up moveing them around invintores and changing vales on your charects, ant it was really laggy on low end comps
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    With unity?

    If so, getting references is one of the most common practices in Unity (just like in other Engines and also in application programming etc), i can't imagine that this was the main reason for the performance issues unless you really pulled many references in Update over and over again or even used alot of GameObject.Find("the name") in the update routines.

    Once you got the reference you wanted, it's simply just like any other variable of the that type, a simple reference to an object (talking about instances of a class here, not GOs). If that would cause lags, you wouldn't even be able to create games in Unity.