Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

ok how do get an object reference to access a non-static member

Discussion in 'Scripting' started by multiplyer, Nov 2, 2015.

  1. multiplyer

    multiplyer

    Joined:
    Jan 28, 2015
    Posts:
    76
    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
     
  2. cowtrix

    cowtrix

    Joined:
    Oct 23, 2012
    Posts:
    319
    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:

    Code (CSharp):
    1. public class Dog
    2. {
    3.     public string Species { get { return "Dog"; } }
    4. // notice this variable isn't static!
    5. }
    we'd do something like this:
    Code (CSharp):
    1. ...
    2. var lassie = new Dog();
    3. Console.Write("I am a " + lassie.Species);
    4.  
    5. // This outputs:
    6. // `I am a Dog`
    7. ...
    Whereas with a static member, declared like so:
    Code (CSharp):
    1. public class Dog
    2. {
    3.     public static string Species { get { return "Dog"; } }
    4. }
    ...we would instead access the variable through the class name, not a reference to a Dog object.
    Code (CSharp):
    1. ...
    2. Console.Write("All dogs are " + Dog.Species);
    3.  
    4. // This outputs:
    5. // `All dogs are Dog`
    6. ...
    Make sense? Again, if you'd like to post your code, we can probably see exactly where you've gone wrong in your logic.
     
  3. multiplyer

    multiplyer

    Joined:
    Jan 28, 2015
    Posts:
    76
    I declared a public int in a script called "headroll"

    Code (CSharp):
    1. public int headroller;
    I then try to reference it in another script by doing this:

    Code (CSharp):
    1. if (headroll.headroller = 5) {
    if you could help me it would be so much appreciated
     
  4. cowtrix

    cowtrix

    Joined:
    Oct 23, 2012
    Posts:
    319
    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.
     
  5. multiplyer

    multiplyer

    Joined:
    Jan 28, 2015
    Posts:
    76
    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?
     
  6. multiplyer

    multiplyer

    Joined:
    Jan 28, 2015
    Posts:
    76
    say I did want to reference that script, though. how would I go about that?
     
  7. cowtrix

    cowtrix

    Joined:
    Oct 23, 2012
    Posts:
    319
    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.
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The most common way to get a reference is GetComponent. There are a bunch of other ways. Here is a video.