#Null refence when scene starting (problem Solved)

1 messages · Page 1 of 1 (latest)

river orbit
#

I have a scene when enter a debuff will be played where if one of the characters is named "Gregor" the debuff will be added to that character delegate the issue is I am getting a null refence and I don't know why it is happening.
There are there scripts in play for this
Board: At start set the two character objects with the character then call their debuff

            SecondPerson = StaticInfo.instance.RightDate;
            person.useDebuff(this);
            person.checkOtherPersonDebuff(SecondPerson,this);
            brokenHeartCallBack += duoHatedTokenEffect;```
DuoPersonCarry: this script job is to track which character is in focus and have a ref to it's person
```if (LeftOrRight)
        {
            person = StaticInfo.instance.leftDate;
        }
        else
        {
            person = StaticInfo.instance.RightDate;
            
        }```
Finally the special debuff with the null error
```DuoPersonCarry[] twoperson = FindObjectsOfType<DuoPersonCarry>();
        foreach(DuoPersonCarry person in twoperson)
        {
            Debug.Log(person.getperson().Personname);
            if (person.getperson().Personname == "Gregor")
            {
                if (person.checkeng == null)
                {
                    Debug.Log("The debuff is added");
                    person.checkeng += allowOrNot;
                }
            }
        }```
so the debug.log is giving me the null error but the weird part is that this is being called twice it is working well for the first one but the error happens on the second person and I don't know why.
near flare
#
  1. You are likely reading the error incorrectly. Your second Log call does not - cannot - throw NRE
  2. Don't use FindObjectsOfType ever
vivid salmonBOT
#

Don't use Find methods!

Find methods in Unity are an often-tempting solution to referencing a scene object. However, there is always a better solution.
Whenever you call a Find method, Unity must traverse your entire scene hierarchy and check every single object until it finds a match; and the methods which return arrays will always traverse the entire hierarchy regardless.
This is inefficient! It means that the more objects you add to your scene, the slower these methods become; and it gets even worse if you are calling them multiple times.

A non-exhaustive list of Find methods to avoid:
https://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html
https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html
https://docs.unity3d.com/ScriptReference/Object.FindObjectOfType.html

To read about a better solution, type []getareference or []getref

near flare
#

Disregard #1, I misunderstood what you meant. I thought you said second Log call (where it prints "The debuff is added"), you meant the second person in the loop

#
Debug.Log(person.getperson().Personname);

So what here has the potential for being null?

river orbit
near flare
#

Okay well let's go through it one by one

#
Debug.Log(person.getperson().Personname);

So we can rule out Personname being null. because if you do Debug.Log(null), it'll just print an empty message. No NRE is thrown

#

So either person or getperson() is returning null

#

Now person can't be null, because FindObjectsOfType wouldn't return a null item. Because Unity considers "null" objects to be non-existent and therefore wouldn't be "found"

#

So the only option left is the getperson() method. That's returning null for some reason. So take a look in there

river orbit
#
    {
        return person;
    }```
near flare
#

Okay sure I guess. so person here is null

river orbit
#

this is the getperson method it is a simple return private method

near flare
#

If you're unsure exactly which person it is that's null, try logging the name of the object

river orbit
#

that is what I am doing right now

near flare
#

Cool

#

You could also pass in a context object to the other Log call

river orbit
#

ok so I notice something odd, the person on the right name is getting called three times

near flare
#

Where did you log the name?

#

If you logged it in getperson method then that makes sense because you're already calling it twice in the loop

        foreach(DuoPersonCarry person in twoperson)
        {
            Debug.Log(person.getperson().Personname);      // <-- here
            if (person.getperson().Personname == "Gregor") // <-- and here
river orbit
#

I also added the debug message in the get person method

#

ok a small bit of good news

#

I change the Duo person carry script a bit to this

        {
            this.person = StaticInfo.instance.leftDate;
        }
        else
        {
            this.person = StaticInfo.instance.RightDate;
            
        }```
near flare
#

You could condense this to one line

#

but that's irrelevant to the issue at hand

river orbit
#

even though I am get the other persons name three times when I stop the test run I got the other persons name

#

I am starting to feel I am not giving the person carry enough time to assign their person

#

I am going to try putting the call debuff method in IEnumerator

near flare
#

Possible. I don't know the structure of your project/game so I don't know when this assignment happens. But all I can tell you is that one of these values is null, either leftDate or RightDate

river orbit
#

ok the waiting for .2f made it work now

#

so ya I really was not giving it enough time to load

near flare
#

Welp

#

Glad you figured it out LUL

river orbit
#

ya but thanks for helping me out

#

but early you said I can condense a line of code in one can you show me how?

near flare
#

Using the ternary operators ? :

if (a)
{
    foo = trueValue;
}
else
{
    foo = falseValue;
}

Can be condensed to

foo = a ? trueValue : falseValue;
river orbit
#

so I can do
this.person = LeftOrRight ? LeftDate : RightDate

near flare
#

indeed

river orbit
#

cool thanks for telling me, and have a nice day

near flare
#

No problem, and you too