- Home /
Running a method from another script with non-static variable returns error
Hi, I've tried searching around for an hour but couldn't get an answer... Either my search skill sucks or there isn't a similar problem.
I'm trying to run ScriptB's method from Script A. Problem is that ScriptB's method contains non-static variables like renderer.enabled and a bool.
This is Script A
void HideDockSign ()
{
dropZone1.GetComponent<SurfaceScript>().DockSign();
}
This is SurfaceScript.cs
public bool occupied = false;
public GameObject dockSign;
public static void DockSign ()
{
if (!occupied)
dockSign.renderer.enabled = true;
}
I do not want to define these variables again from Script A because there are other 8 similar items, and that would make a mess. Any ideas? Thanks!
In principle calling another script's function with non-static variables shouldn't be a problem.
Is there an objected instantiated of the class that contains DockSign()?
EDIT: I guess there are, according to the end of your post. Hmm. Does the function have to be static? Seems odd to me.
I mean, if you have 8 objects and they all share the same attribute-values, I'd just make the attributes static. If they don't share the same values, make the method non-static and call it for each object.
No, there is no object instantiated. It's just those that you see.
Reason for the function to be static is because there was an error, but it disappeared now that i removed the static.
The error returns for the dockSign.renderer
error CS0120: An object reference is required to access non-static member SurfaceScript.dockSign' and the occupied bool error CS0120: An object reference is required to access non-static member
SurfaceScript.occupied'
$$anonymous$$y apologies... after removing the static from the function, it really worked.....
I hate it when I tried to solve something for so long, and wasted people's time trying to help over a small mistake...
sorry man, and thanks for the help ^^
Answer by raimon.massanet · Nov 27, 2013 at 08:51 AM
You are trying to call a static function DockSign
from a non-static context (a particular instance of the script). To run DockSign
from HideDockSign
just do:
void HideDockSign ()
{
SurfaceScript.DockSign();
}
On the other hand, a static method like DockSign
can't access non-static variables like dockSign
.