Search Unity

Question public GameObject player

Discussion in 'Scripting' started by looot1985, Jun 11, 2021.

  1. looot1985

    looot1985

    Joined:
    Jun 11, 2021
    Posts:
    12
    Create a new script for the camera section of the unity intro course.

    In the video, he calls it variable but there is no = for the assignment.
    so I'm confused is it a variable or something else

    Code (CSharp):
    1. public class FollowPlayer : MonoBehaviour {
    2.     public GameObject player; // don't understand this
    3.  
    4.     // Start is called before the first frame update
    5.     void Start() { }
    6.  
    7.     // Update is called once per frame
    8.     void Update() { }
    9. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    By convention in Unity, fields of this kind are typically assigned in the inspector, AFTER you place an instance of this script in your scene or on your prefab.

    That way when Unity "stands this object up," it will inject that assignment that you made and saved in the scene.

    If you fail to assign this, then yes, it would be null, and you would get a nullref.
     
  3. looot1985

    looot1985

    Joined:
    Jun 11, 2021
    Posts:
    12
    @Kurt-Dekker

    But how, is it a constructor, abstraction, etc how is it a variable with no assignment.
    is it just what unity does behind the scenes?

    like
    Code (CSharp):
    1.  
    2.                 //This vector3
    3. private readonly Vector3 _offset = new Vector3(0, (float) 6.19, (float) -14.34);
    the Vector3 is that a variable too like double variables are a thing?
     
    Last edited: Jun 11, 2021
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
  5. looot1985

    looot1985

    Joined:
    Jun 11, 2021
    Posts:
    12
    I read it, but still dont understand how can you have 2 variables
    1. Code (CSharp):
      1. private readonly Vector3 _offset = new Vector3(0, (float) 6.19, (float) -14.34);
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    I clearly don't understand your question.

    Code (csharp):
    1. public GameObject player1;
    2. public GameObject player2;
    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220
     
  7. looot1985

    looot1985

    Joined:
    Jun 11, 2021
    Posts:
    12
    Code (CSharp):
    1. private Vector3 _offset = new Vector3(0, (float) 6.19, (float) -14.34);
    2.  
    3. //Vector3 is it a variable?
    4. // _offset is this a variable too?
    5. // how can you have 2 variables
    6. // forget about the first question
    7.  
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    If you are asking these types of questions, you are not going to get any benefit from my response.

    In the example above, Vector3 is a type that Unity provides and _offset is a private instance variable of that type.

    We could go on indefinitely with random people here 'asplainin' random parts of C# to you, but I have much better things to do so I suggest you work through a few dozen of these tutorials:

    Imphenzia / imphenzia - super-basic Unity tutorial:



    Brackeys super-basic Unity Tutorial series:



    Sebastian Lague Intro to Game Development with Unity and C#:



    How to do tutorials properly:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation generally ends in disaster. That's how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right. Be a robot. Don't make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
  9. looot1985

    looot1985

    Joined:
    Jun 11, 2021
    Posts:
    12
    Like I know some programming (javascript} but will take your suggestions. so gameObject is another unity type? If so I understand now thanks
     
  10. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    I'm guessing this may be your first time using a strongly-typed language then?

    In JavaScript, you can give any type of value to any variable like so:
    Code (JavaScript):
    1. let x = 25;
    2.  
    3. let y = "hello";
    4.  
    5. x = true;
    6.  
    7. y = [];
    This is what's known as a loosely-typed language.
    I.E: a variable can be of any type: a number, a string, an object, etc.

    C# and some other languages, however, are strongly-typed, which means you need to explicitly state whether a variable is a number, a string, an object, etc.
    And once you declare a variable's type, it cannot change types afterwards.

    In C#, you declare the type of a variable before it's name:
    Code (CSharp):
    1. int x = 25; //This is a variable named "x" and it is an integer type.
    2.  
    3. string y = "hello"; //This is a variable named "y" and it is a string type.
    4.  
    5. //"x" can be assigned any other integer value.
    6. x = 3;
    7. x = 82;
    8. x = -261;
    9.  
    10. //"x" CANNOT be assigned any value that is not an integer.
    11. x = true;
    12. x = "hello";
    13.  
    14. //"y" can be assigned any other string value.
    15. y = "Hi.";
    16. y = "Good morning."
    17. y = "Bye."
    18.  
    19. //"y" CANNOT be assigned any value that is not a string.
    20. y = 32;
    21. y = false;
    So to clarify,
    Code (CSharp):
    1. GameObject player;
    "GameObject" is the type, and "player" is the name of the variable.
     
    Kurt-Dekker likes this.
  11. looot1985

    looot1985

    Joined:
    Jun 11, 2021
    Posts:
    12
    Thank you!