1

I am working on a game and i have a script to save the stats for characters. The same script is used for hero and for the enemies.

There are actually 2 scripts. 1st script is for game settings which makes sure that which player is to be set visible and which enemy to set visible. That script has variables like

[Header("Players")]
    public GameObject LocalPlayer;
    public GameObject Enemy;

Now in my 2nd script with name User_stats, i want to put target for hero = enemy and vice versa. What i am doing in my 2nd script is that i calling my 1st script (FKManage) in it by doing following code

private FKManage FKManage; Then in my void start, I am using a method named SetTarget(); which is defining these two arrays and telling them that the objects with tag player need to have Target = Enemy from the FKManage script and vice versa

GameObject[] protagonistOpponent = GameObject.FindGameObjectsWithTag("Player");
        foreach (GameObject obst in protagonistOpponent)
            obst.GetComponent<User_Stats>().Target = FKManage.Enemy.transform;

        GameObject[] AntagonistOpponent = GameObject.FindGameObjectsWithTag("Enemy");
        foreach (GameObject obst in AntagonistOpponent)
            obst.GetComponent<User_Stats>().Target = FKManage.LocalPlayer.transform;

And it works fine but only in Unity editor. As soon as i build the game for Android and run it under Android, my characters don't do anything (because target is not set).

I want to know why my build is not working right as it is working in editor? Where am i making the mistake? On building the game, this is the error i get in my android logcat.

enter image description here

Aykut Karaca
  • 350
  • 3
  • 15
Rameez Safdar
  • 21
  • 1
  • 7
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – BugFinder Mar 16 '20 at 10:13
  • Hi, Thank you for your help. I have read the whole page and yes i understand what it says but i think this doesn't solve my problem, because my game does not set a null reference in editor but it does in game build. Also please tell me how is this even possible? i am kinda new to Unity and am learning, i mean if my game works fine in editor and not in build, this way i can never be sure if my game is okay and will work on phone or not without making a build every time i make a small change. Sorry for my long texts tho. I am stuck with this error and it's been around a week now. – Rameez Safdar Mar 16 '20 at 10:37
  • it means something didnt work when not running on your pc so maybe player preferences, access to files, webserver response, theres no way anyone here can guess what dependancies you have, but something isnt working and is therefore not working. a number of things dont produce errors in the same way as a result sometimes start I think doesnt tell you it errors and so if in start you assign a load of stuffl, if that doesnt work then those dont get set.. – BugFinder Mar 16 '20 at 11:02
  • Do you try to load a file or object from resources in your script instead of assigning it at inspector? Android file system doesnt work as Unity expects and it maybe looking at wrong location for that file. I had a similar issue with loading my json file. I figured it out so depending on your answer I can have a look at my solution. – Aykut Karaca Mar 16 '20 at 11:11
  • Hi @AykutKaraca I guess you're right. I did not assign the value in inspector. I am getting that value from another script which gets loaded with the scene. And i think you are right that android system doesnt work as Unity does because in Unity, my script is successfully getting the GameObject from other script while android Build is not. Please explain how did you fix it ? – Rameez Safdar Mar 16 '20 at 11:14
  • @BugFinder If we look at the Android LogCat we can see that FKManage.start(); starts after the User_stats.Target(); Where it should start before User_stats.Target(); which it does in editor but in build it starts after it, what sorcery is this ? – Rameez Safdar Mar 16 '20 at 12:07
  • it depends, it could be timings it could be execution order, you've already alluded it could be not finding information.. – BugFinder Mar 16 '20 at 12:18
  • @BugFinder How to make sure that in build it executes the scripts as it does in Editor. Is there a way to find or fix it? Also if rather than setting values in start if i set it in void update it might work but that will not be an efficient technique, right? – Rameez Safdar Mar 16 '20 at 12:26
  • no, but you can st execution order, there is lazy loading.. theres a ton of things but without details.. you need to research your choices and evulate what is right – BugFinder Mar 16 '20 at 13:14
  • please show the code where you load the gameobjects dynamically. If you use Resources.Load(), the file should be under "/Resources" folder in your project so that it also works under Android build. For example if you use this code: this.Sprite = Resources.Load("Sprites/Items/" +spritename); then your sprite file must be under "Resources/Sprites/Items/" – Aykut Karaca Mar 16 '20 at 13:19
  • Rameez, when an error occurs, Unity may skip next lines of code and you may get strange errors elsewhere. Therefore may look like change in the execution order. It took me several months to solve my error because it was giving other errors where it was actually a json file not uploaded and therefore my database for inventory wasnt built. – Aykut Karaca Mar 16 '20 at 13:28
  • Hi @AykutKaraca I am calling the gameobjects dynamically in **FKManage** script like this `[Header("Players")] public GameObject LocalPlayer; public GameObject Enemy; public GameObject[] Protagonists; public GameObject[] Antagonists; private int index; private int indexant;` – Rameez Safdar Mar 16 '20 at 13:37
  • Then in **FKManage.Start** method i do this to assign 1 value for localplayer and enemy out of the array of gameobjects. `index = Random.Range(0, Antagonists.Length); foreach (GameObject go in Antagonists) go.SetActive(false); if (Antagonists[index]) Antagonists[index].SetActive(true); Enemy = Antagonists[index]; indexant = PlayerPrefs.GetInt("CharacterSelected"); foreach (GameObject gop in Protagonists) gop.SetActive(false); if (Protagonists[indexant]) Protagonists[indexant].SetActive(true); LocalPlayer = Protagonists[indexant];` – Rameez Safdar Mar 16 '20 at 13:44

2 Answers2

0

So i have fixed the issue.

What i have done is i have changed the script execution order, a really simple fix and works like a charm. Read about how to change script execution here

Thank you everyone for helping me out especially @BugFinder and @AykutKaraca

Rameez Safdar
  • 21
  • 1
  • 7
0

For any one who still doesn't solved the problem, check if you have code like Debug.Assert(). Such debug code (methods like print() excluded) will be deleted in the build version.

Gytobp
  • 1
  • 1