C
C#4mo ago
GregTheEgg

✅ Variable access help :(

I have an object named player and I'm trying to access a variable, an int, called speed and I've been told to use "GetComponent" although when I use it I'm a tad bit confused on how to access the variable? "speed = player.GetComponent<speed>();" is what I tried which didn't work.
No description
17 Replies
Buddy
Buddy4mo ago
speed is not a component, it is a variable / field.
GregTheEgg
GregTheEgg4mo ago
oh so I put the script I'm trying to access in the <>
Buddy
Buddy4mo ago
Components are classes that inherits MonoBehavior You can access speed directly. Since it lives within the same class
GregTheEgg
GregTheEgg4mo ago
oh shoot the speed in phones isn't what I'mt rying to access
GregTheEgg
GregTheEgg4mo ago
No description
GregTheEgg
GregTheEgg4mo ago
trying to access this speed which is in basic movement in a seperate script
Buddy
Buddy4mo ago
I see. Then you use GetComponent<BasicMovement>() It is the name of the class / component
GregTheEgg
GregTheEgg4mo ago
ah ok that worked but then how do I get access to the individual variables?
Buddy
Buddy4mo ago
Then you can access speed from there
GregTheEgg
GregTheEgg4mo ago
so it's entirely connected now? or atleast the publics are?
Buddy
Buddy4mo ago
BasicMovement movement;

void Start()
{
movement = GetComponent<BasicMovement>();
}
BasicMovement movement;

void Start()
{
movement = GetComponent<BasicMovement>();
}
And then you can access speed by doing movement.speed
GregTheEgg
GregTheEgg4mo ago
oh ok
Buddy
Buddy4mo ago
If it lives on the same object in the hierarchy
GregTheEgg
GregTheEgg4mo ago
I understand now, thanks 🙂 should I delete my post now or do I just leave it
Buddy
Buddy4mo ago
$close
MODiX
MODiX4mo ago
If you have no further questions, please use /close to mark the forum thread as answered
GregTheEgg
GregTheEgg4mo ago
ah cool thanks again
Want results from more Discord servers?
Add your server