like for real this is just so awful when it happens. it makes perfect sense in the code then I get this. please can someone help this happens in anything I do
Hey multiplyer. I think first we need to explain some stuff about what these terms mean. Your code is probably doing exactly what its supposed to (although it would help a lot if you posted it so we could see exactly where you’re going wrong), and it doesn’t actually make sense.
Firstly, what’s a static member? A static member is, in a design sense, a member that is relevant and the same for all instances of a class. Say I have a class named Dog, with a member that returns their Species. All dogs are the same species, so we can make Dog.Species a static variable.
Secondly, what’s an object reference? When we use the new keyword, we make an object. When we say var lassie = new Dog(), we make a dog, and we give it an object reference called lassie. Now we can get this thing we’re calling lassie to do stuff - bark, run around, eat. But to do that, we need to know where lassie is. This is what the reference is doing - pointing to the bit of memory on our computer that lassie is in, so we can do stuff to it.
Consider this Dog class, with a non-static member Species:
public class Dog
{
public string Species { get { return "Dog"; } }
// notice this variable isn't static!
}
we’d do something like this:
...
var lassie = new Dog();
Console.Write("I am a " + lassie.Species);
// This outputs:
// `I am a Dog`
...
Whereas with a static member, declared like so:
public class Dog
{
public static string Species { get { return "Dog"; } }
}
…we would instead access the variable through the class name, not a reference to a Dog object.
...
Console.Write("All dogs are " + Dog.Species);
// This outputs:
// `All dogs are Dog`
...
Make sense? Again, if you’d like to post your code, we can probably see exactly where you’ve gone wrong in your logic.
I declared a public int in a script called “headroll”
public int headroller;
I then try to reference it in another script by doing this:
if (headroll.headroller = 5) {
if you could help me it would be so much appreciated
Ok, so because headroller isn’t a static variable, you can’t access it through the class name. You’re saying that the value of headroller could change between instances of headroll. If you don’t want that behaviour, change headroller to a static variable.
If you do want headroller to be different between instances, you need to make a headroll object, keep a reference to it, and then ask that instance about what headroller is.
the main reason I didn’t want it to be static is so I could check to see if the funcion was working in the inspector. would there be a way to check if it worked in debugging or something?
say I did want to reference that script, though. how would I go about that?
Yes, you can check it in the editor in a number of ways, through Debug.Log() or a CustomEditor.
You can pretty much use the examples I gave in my first post - in this case, headroll = Dog and headroller = Species.