0

Trying to pass the object reference and set it in the new script/class. Player.cs

How do i go about referencing correctly, and assigning

user3072143
  • 77
  • 1
  • 2
  • 7
  • You don't need to instantiate `Player` when you want to just throw it away in the next line. `Player = NewPlayer;` – Bizhan Jul 03 '18 at 23:13
  • so according to the photo, `Player.Achievement` is not instantiated. instantiate `Player.Achievement` and you're good to go – Bizhan Jul 03 '18 at 23:15
  • That was an error, was testing stuff. here is where it all goes wrong https://puu.sh/AQune/9ff16e6b16.png AchievementList is a Unity text field – user3072143 Jul 03 '18 at 23:22
  • select the game object with `PanelUpdate` attached to it. then find `AchievementList` in inspector. its value should be . drag and drop the UI text on it – Bizhan Jul 03 '18 at 23:29
  • 1
    found yet another error: `public PanelUpdate(PlayerDetails NewPlayer) { PlayerDetails Player = new PlayerDetails(); Player = NewPlayer; }` is wrong. constructors are not functional in monobehaviour – Bizhan Jul 03 '18 at 23:37
  • Could you edit your question and add some more code . – Ginxxx Jul 04 '18 at 02:30

2 Answers2

0

I believe this is one of those nasty instances of a NullPointer exception being able to refer to multiple problems. It only gives you the line number, but it doesn't give you which reference in that line was null. NewPlayer isn't null, AchievementList is. You need to initialize that value either earlier in the ShowUI method or in the PanelUpdate constructor.

P.S. I HATE the non-specificity of null reference exceptions; very frustrating.

Semimono
  • 664
  • 1
  • 6
  • 14
  • So i made the constructor and passed PlayerDetails to it and assigned it to a new one, still not sure, but its still showing 'this' as null https://puu.sh/AQtUk/b58c3360dc.png – user3072143 Jul 03 '18 at 23:03
  • Yes, you can't pass a reference of an object until the constructor has finished. You can use `this` to assign values (aka this.Player =...), but you can't assign values to `this` (aka myVar=this). I think this representation is just how the debugger shows it. – Semimono Jul 03 '18 at 23:10
  • But I think you might have missed what I was trying to point out. You need to initialize `AchievementList` in the constructor, not `NewPlayer` or `Player`. `AchievementList` is the value that is null. – Semimono Jul 03 '18 at 23:10
  • Cant initialize achievement list. its a unity text field, trying to sets the same error anyways – user3072143 Jul 03 '18 at 23:16
  • @user3072143 i think he meant NewPlayer.Achievement – Bizhan Jul 03 '18 at 23:18
  • https://puu.sh/AQuvK/13ecfb1b7a.png Did so, same problem – user3072143 Jul 03 '18 at 23:26
0

Your PlayerDetails object has a null property in it. Check the properties on the NewPlayer object in debug mode and you will see this.

Even if you use a new Constructor on it it doesn't mean the property will be given a value unless it's specifically given one within the constructor.

You also need to show the code for the PlayerDetails class to give us a better idea.

Also:

PlayerDetails Player = new PlayerDetails(); // Assigning a new value here means nothing
Player = NewPlayer;

You're creating a new PlayerDetails object on line #1 but then assign it a different value on line #2.

PlayerDetails Player = NewPlayer;

Is fine.

C. Carter
  • 291
  • 2
  • 11