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