Monday, October 31, 2022
HomeGame DevelopmentFind out how to entry a variable from one other script in...

Find out how to entry a variable from one other script in Unity c#


What you want are getters and setters. Mainly it’s a manner of getting and/or setting variables in lessons (or scripts on this case) from exterior code. There are numerous ways in which you are able to do this:

Computerized get and set

By altering the code to the code beneath you make the variable accessible to the surface lessons (public get;) however these lessons cannot change the variable. Which you may want so you do not change or not it’s accident.

[SerializeField]
non-public float InspectSpeed;

public float velocity { get; non-public set; }

void Begin () {
    velocity = InspectSpeed;
}

So that you would possibly discover this can be a lot of code, Sure…. It’s. Whereas automated setters and getters are superior for variables you need to share between lessons/scripts the inspector will not mean you can use them (which is why i assume velocity is public) so I added an additional float which is seen to the inspector. Since that is then not the identical as velocity it’s worthwhile to set it at startup.

To then entry the velocity variable you merely get it from the occasion of the category:

transfer.velocity;
//notice you will not have the ability to change the variable because the setter is non-public

Handbook getter

You would simply add a way which has a return worth of float which then all the time returns the variable of velocity. When each you want velocity simply name the tactic

Public float getSpeed(){
    return velocity;
}

so from one other script/class you’ll name:

transfer.getSpeed();
//returns the velocity

Making the variable public (not advisable)

You will have already executed this within the code pattern that you simply supplied. You declared the velocity variable as public which means that each one scripts can simply name the variable from wherever. I believe you probably did this to make it seen within the inspector (we’ve got all executed it sooner or later :)).

The issue with that is that as you add increasingly more variables to scrips that you simply need to be changeable within the inspector it turns into more durable and more durable to work with the code as you add extra scripts. It is because when it’s worthwhile to name strategies in different lessons or scrips it’s going to present each public variable in addition to all the opposite public strategies that you’ve got. Eventually you’ll have 2 variable of the identical title at which level will probably be differ complicated which variable you’re getting if you’re getting information of the identical title from a number of sources.

To repair this make each variable non-public and add “[Serializable]” above it which can stop different lessons/scripts from seeing it however the inspector will nonetheless have the ability to work together with it.

And only for the completion of this level you entry the velocity variable like this:

transfer.velocity;

This could cowl all the pieces, if it does not work for no matter cause remark bellow 🙂

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments