C
C#5mo 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
Buddy5mo ago
speed is not a component, it is a variable / field.
GregTheEgg
GregTheEggOP5mo ago
oh so I put the script I'm trying to access in the <>
Buddy
Buddy5mo ago
Components are classes that inherits MonoBehavior You can access speed directly. Since it lives within the same class
GregTheEgg
GregTheEggOP5mo ago
oh shoot the speed in phones isn't what I'mt rying to access
GregTheEgg
GregTheEggOP5mo ago
No description
GregTheEgg
GregTheEggOP5mo ago
trying to access this speed which is in basic movement in a seperate script
Buddy
Buddy5mo ago
I see. Then you use GetComponent<BasicMovement>() It is the name of the class / component
GregTheEgg
GregTheEggOP5mo ago
ah ok that worked but then how do I get access to the individual variables?
Buddy
Buddy5mo ago
Then you can access speed from there
GregTheEgg
GregTheEggOP5mo ago
so it's entirely connected now? or atleast the publics are?
Buddy
Buddy5mo 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
GregTheEggOP5mo ago
oh ok
Buddy
Buddy5mo ago
If it lives on the same object in the hierarchy
GregTheEgg
GregTheEggOP5mo ago
I understand now, thanks 🙂 should I delete my post now or do I just leave it
Buddy
Buddy5mo ago
$close
MODiX
MODiX5mo ago
If you have no further questions, please use /close to mark the forum thread as answered
GregTheEgg
GregTheEggOP5mo ago
ah cool thanks again
Want results from more Discord servers?
Add your server