- Home /
Everything working fine in Editor but in device the find object with tag throwing null exception
Hi, I am using Unity 5.5.1p1 I have upgraded my project from unity 5.3 to Unity 5.5.1p1. I am working currently on the android game. I am facing some strange issue, everything is working fine when I test my game in unity editor but when I build and deploy the game in the device it throws a null exception particularly where I have used find gameobjects with the tag.
If I replace it with gameobject.find("object name"). It works fine on the device too. But as it is not convenient for me to now replace it all gameobjects with the tag with gameobject .find(). Please reply as soon as possible if anyone has solution or same issue.
Thanks in Advance.
Answer by fincottle44 · Mar 14, 2019 at 09:48 PM
For anyone encountering this problem:
This happened to me, it was due to having a (removed) tag in the editor.
My only guess is that the editor can handle this but not a build, not sure. You've probably tried it but worth a go!
Restart unity.
Answer by surfuay · Mar 15, 2019 at 12:21 AM
so one of the things that's really big with gameobject.find is where you have it. Usually you have it at the start method, oncollision or ontriggerenter.
If you don't have a handler (variable declared at the beginning ie private Player player) for the game object and then a declaration to find it somewhere such as
private void start() { //needs to be capitalized player = GameObject.Find("name of thing you're finding") and then the GetComponent of that game object you specifically are wanting, it won't work. }
Personally I like to use static
public static Player ninja;
public so any script can access any compenents within that player script static so that it doesn't change throughout movement of of anything else in the app Player which happens to be the name of my script so what ever script you're in will be that 3rd word ninja just the name i gave it within that sript could have been steve
so now you need 2 more things
in the script you declared static you need the following Method at the beginning, this will make sure that you only ever have one of it in existence at a time (works even if i do this with enemies and spawn in 5000 of them, it just makes sure that there is only one instance of each desired instantiation
private void Awake() {
if (ninja == null)
{
ninja = this;
}
else if (ninja != this)
{
Destroy(gameObject);
}
}
then in whatever script you were trying to find the game object you would simply write this line and adjust it accordingly
Player.ninja.lives = 4;
or Player.ninja.Movement();
so the first word is the name of the Script the second is what you decided to name the script the last part is the component of the script you're trying to access.
it can be longer than 3 pieces but the first 2 are always CLASS.NAME.everything else.
if you're deep in your game, from experience, it can be really cumbersome to translate everything into Singletons (which is what i am recommending)
but for GameObject.Find() you'll need the following lines of GetComponent("component").whatever the rest of your compoenent is