Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

how can i refrence a value in a another script?

HI! I need to refrence a value (int and a double) from a another script and i don't know how can i do that. Is there a way to refrence them or do i need to get them in the same script? I use C#

Best Answer

  • MouledouxMouledoux Member
    Accepted Answer

    There are plenty of ways to go about this. Are you working in Unity, or just C#?

    Also, are the values static? If they are, you can just do [ClassName].[VariableName] but that's only if they're static. They're probably not though, in which case, you'll need to get a reference to an INSTANCE of the class. An instance, just FYI, is when you make a variable of the class type. You could make that instance inside the class you need the variables in. If you're in Unity, you can set this in the inspector.

Answers

  • ExainzExainz Member

    Can go with

    Findobject of type

    And then the script..


    Aur just inherit the parent script...

  • ZicrusZicrus Member
    edited June 2020

    Please don't use findobjectoftype, getcomponent or anything similar, because they are really performance heavy. Make a variable with the type of your other script/class:

    Otherscriptname otherscriptname;

    Either initialise in the inspector, or if you want to make a new instance (or don't use unity) write:

    otherscriptname = new Otherscriptname();

    Then access the other scripts int:

    otherscriptname.nameofvariable

Sign In or Register to comment.