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

Function to make a game objects in a scene using a class, then edit those objects properties.

Discussion in 'Scripting' started by JalexRas, Apr 4, 2020.

  1. JalexRas

    JalexRas

    Joined:
    Mar 30, 2020
    Posts:
    6
    I'm trying to declare game objects using a function to make an instance of a class that has and can change the properties of the gameobject, but I get the error

    NullReferenceException: Object reference not set to an instance of an object
    Control.initPlayer (Control+chara Chara, System.String charaName, UnityEngine.GameObject charaGO, System.Boolean charaCJ) (at Assets/Control.cs:114)
    Control.Start () (at Assets/Control.cs:29)

    I'm unsure why this doesn't work. Any advice would be helpful. If you have example code that would be extremely helpful.


    Code (CSharp):
    1.  
    2.  
    3. public class chara
    4.         {
    5.             public string name;
    6.             public GameObject go;
    7.             public Rigidbody rb;
    8.             public CharacterJoint cj;
    9.         }
    10.  
    11.         public void initPlayer(chara Chara, string charaName, GameObject charaGO, bool charaCJ)
    12.         {
    13.             Chara.name = charaName;
    14.             Chara.go = charaGO;
    15.             Chara.rb = charaGO.AddComponent<Rigidbody>();
    16.             if(charaCJ == true)
    17.             {
    18.                 Chara.cj = charaGO.AddComponent<CharacterJoint>(); ;
    19.             }
    20.          
    21.         }
    22.  
    23.  
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    There is very little we can help. You are not showing the correct script, and the script you are showing seems heavily edited (no using, and it's apparently not a MonoBehaviour). The error in the Control.cs script in the Control.InitPlayer() method. You are showing script called 'chara'.
     
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    The snippet actually contains the important part of the script the error is referring to, as you can see in the stacktrace.

    It's pointing to "initPlayer" which appears to be a member of a type named Control.

    As for the problem in this code:


    it's indeed difficult to guess which line maps to line 114 that is reported in the stack trace.
    However, you can quickly tell that it can only be one of the following:

    - argument 'Chara' is null
    - argument 'charaGO' is null

    because there's an attempt to access members of both. The rest of the lines shown are only assignments to field and thus to not cause such exceptions. The only question remains is, which of one both is null? Check line 114 in the original code, or use the debugger, or check which actual arguments are passed to 'initPlayer'.

    That's as much as we can help you here.
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    @Suddoha Thank you for pointing that out. It looks like I may learn something new (this is not sarcastic, I am hopeful that I get a better understanding here), as I may have gotten something wrong (I often do :) ). I thought that

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. Control.initPlayer (Control+chara Chara, System.String charaName,
    3. UnityEngine.GameObject charaGO, System.Boolean charaCJ) (at Assets/Control.cs:114)
    meant:
    • The code tries to access location null (zero) while dereferencing a pointer
    • (?) This was inside the parameter bock of the initPlayer method belonging to a type of class named 'Control'
    • The parameter signature of that method is (Control+chra, String, GameObject, Boolean)
    • The error happened on line 114 of the script called 'Control' that is located in the Assets folder of the project
    OP shows a Chara class that (incidentally?) also has an initPlayer method with same signature. I thought that they showed the wrong class because ot the 'Control' reference.

    I now realize that I may be confused simply because I define a single class in each script and therefore have a 1:1 relationship between class name and script file name. It may be that 'char' is another class also defined in the 'Control.cs' script, and therefore my assupmtion is wrong. So I guess the question is : Does the reference to 'Control.initPlayer' invocation in the stack trace refer to a class.method invocation, or it a reference to the script file name itself (the reference to asset/control.cs clearly is, but the first I think is not)? In the latter case, I'm mistaken. If not, if the error happened in the 'chara' class, shouldn't the stack trace have flagged that as

    chara.InitPlayer

    and not Control.initPlayer? That is the entire reasonwhy I claimed that we were shown the wrong code.

    From the error shown all I could deduce that any object in that invocation could be null: Chara, charaName, charaGO, CharaCJ - with charaCJ being unlikely as few people box their bools, and the compilers parser would likely have caught it).
     
  5. JalexRas

    JalexRas

    Joined:
    Mar 30, 2020
    Posts:
    6
    Line 114 is
    Code (CSharp):
    1. chara.name = charaName;
     
  6. JalexRas

    JalexRas

    Joined:
    Mar 30, 2020
    Posts:
    6
    and control is the name of the script
     
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Your understanding is correct.

    It's just the he has thrown the offending pieces together into a single snippet. Perhaps they are also in the same file, maybe it's even a nested type. We don't know, but it's not important in order to guess what's going wrong here.


    The former. It's the type's name and the method's signature. As you've seen, the file name is appended. If this was an assembly, it would have mentioned that as well.
    Only Chara and charaGo can cause this error, because the code tries to access members of those. charaName is only used for an assignment, no member is accessed so that's totally valid.

    Even if the bool was boxed on the callsite, it would have required unboxing in order to load it as the argument into the stack frame, because the parameter is declared as value type. So that one could not have caused this exception either.[/user]
     
  8. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    That'd mean chara (Chara in your original snippet) is null.