- Home /
The question is answered, right answer was accepted
Build only script error
I have a project that behaves normally in the Editor mode when I play test it, but when I build it and then test the same project, I get a reference error on a script that doesn't produce this error in the editor, nor should it produce this error in the build to the best of my knowledge; I've included the code I'm using for a reference:
public class JumperInventory : Inventory
{
GameObject player;
private void Awake()
{
player = GameObject.FindGameObjectWithTag("Player");
}
protected override bool AddItemToArray(InventoryItem itemToAdd, int quantity)
{
player.GetComponent<CharacterJump>().NumberOfJumps++;
player.GetComponent<CharacterJump>().NumberOfJumpsLeft++;
Debug.Log("New Jump?");
return base.AddItemToArray(itemToAdd, quantity);
}
}
What I'm trying to do is have this of course increase the number of jumps when it gets picked up, however when I run it in the build it always points to a null reference on the NumberOfJumps and NumberOfJumpsLeft only in the build, in the editor it works fine.
Would anyone know why it would behave differently in the build versus the editor, or provide any insight as to why it has trouble finding reference in the build versus the editor?
Answer by SkaredCreations · Oct 09, 2018 at 02:57 PM
May be that :
it's getting a different gameObject that also has the tag "Player" but hasn't the component CharacterJump attached
the player object is not found in the scene when Awake is executed
The NullPointException let us suppose that player is null or it hasn't the CharacterJump component attached.
Anyway you should optimize your code by caching CharacterJump instead of searching for it every time you call AddItemToArray:
GameObject player;
CharacterJump characterJump;
void Awake()
{
player = GameObject.FindGameObjectWithTag("Player");
if (player != null)
characterJump = player.GetComponent<CharacterJump>();
}
protected override bool AddItemToArray(InventoryItem itemToAdd, int quantity)
{
if (characterJump != null)
{
characterJump.NumberOfJumps++;
characterJump.NumberOfJumpsLeft++;
}
Debug.Log("New Jump?");
return base.AddItemToArray(itemToAdd, quantity);
}
After posting this yesterday I decided to scale back and use a simple cube object and noticed this script worked on that ins$$anonymous$$d of my player prefab I had been testing with; come to find out one of the child objects of my player prefab had the tag player which of course didn't have the component to it.
It's always something so insanely stupid that trips me up the most! Thanks for the reply, it was your first answer!
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
[Unity 2017.3.0f3] Cannot open my build it Crashes (with crash Raport) 1 Answer
ios build problem 1 Answer
Not detecting touch in android device if Gradle build system with Build app bundle (Google play) 0 Answers
[Cloud build] iOS build error after changing compilation settings from Xcode 8.3 to Xcode 9+ 0 Answers