C
C#10mo ago
discount

help needed with accesing a variable in another script

im trying to acces isgrounded in another script and have it be the same as hi but i get this error
No description
No description
10 Replies
discount
discountOP10mo ago
fallingplatform is the name of the script i wrote this in playerscript is another script
Angius
Angius10mo ago
Well, access has not been initialized with any value And does playerscript class even have an isGrounded member? Also, is that Unity?
discount
discountOP10mo ago
yea and yea its unity
No description
Angius
Angius10mo ago
That is a method Methods are called IsGrounded() (they're named with PascalCase too, btw) (just like classes)
discount
discountOP10mo ago
ooooh do you know how i can us the method in the other script
Angius
Angius10mo ago
Considering how it's an instance method, you will need an instance of that class If it's in a different namespace, that namespace will need to be imported And, well, the method needs to be called of course .IsGrounded() not .IsGrounded
discount
discountOP10mo ago
i have no idea how to do that
Angius
Angius10mo ago
var script = new PlayerScript();
var grounded = script.IsGrounded();
var script = new PlayerScript();
var grounded = script.IsGrounded();
for example
discount
discountOP10mo ago
i get an this error twice The contextual keyword 'var' may only appear within a local variable declaration or in script code A field initializer cannot reference the non-static field, method, or property 'fallingplatform.script' i also get this one
Kouhai
Kouhai10mo ago
The contextual keyword 'var' may only appear within a local variable declaration or in script code Means you can't do
class Foo
{
var bar = ""; // Compile time error
}
class Foo
{
var bar = ""; // Compile time error
}
A field initializer cannot reference the non-static field, method, or property 'fallingplatform.script' Means you can't do
class Foo
{
string val = GetName(); // Compile time error
string GetName()
{
return "John Doe";
}
}
class Foo
{
string val = GetName(); // Compile time error
string GetName()
{
return "John Doe";
}
}

Did you find this page helpful?