Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mitshah97 · Apr 02, 2017 at 10:03 AM · scripting problemunity5android buildfindgameobjectswithtag

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 5.3.3f1: How to set an object as AssetBundle by script? 0 Answers

Variable only changes for one object that has the script attached and not the other objects with the same script. 1 Answer

Unity game Hanging on Android Device but Windows Working Perfect 0 Answers

Decal Rotation after instantiation 0 Answers

il2cpp.exe did not run properly! when build Android app. 6 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges