Array works in editor, not in build

I am trying to turn off all instances of game objects tagged “infoCard”, then turn one back on based on a UI click. The code I made works as expected when played from the editor, but not when I build. Any ideas why?
Thanks in advance!

the code is as follows-

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class triggerCardFromProduct: MonoBehaviour
{
public GameObject theInfoCard;
private GameObject turnOffCards;

	void OnTriggerEnter(Collider other){
turnOffCards = GameObject.FindGameObjectsWithTag ("infoCard");

	{
		for (int i = 0; i < turnOffCards.Length; i++)
			turnOffCards *.SetActive (false);*
  •   }*
    

if (other.CompareTag (“MainCamera”))

  •   {*
    
  •   	theInfoCard.SetActive (true);*
    
  •   }*
    
  • }*
    }

3 Answers

3

I can’t answer your question exactly, but I can give you a solution that will fix a major problem with your code anyway, which is that it relies on tags. Tags provide the functionality that you’re looking for but it’s much much better for you to manage those objects yourself. Doing so will likely resolve your problem.

You should consider making a “singleton” class. This is a Manager class that has itself as a static member. So it would go like this (pseudocode).

YourManager Class : Monobehavior
{
     public static YourManager instance;
  public List<GameObject> yourObjectlist  = new List<GameObject>();
      void Awake()
      {
          instance = this;
       }
}

OK, so now all you do is add those “infoCard” objects to yourObjectList in their start function with YourManager.instance.YourObjectList.Add(gameObject). Now you can access that List from anywhere in code.

In your trigger enter function you would then iterate through YourManager.instance.YourObjectList and set all those objects to inactive. You could do other comparisons this way.

Are all of the assets being loaded by this array in the /Resources folder? If you have some resource types not within the /Resources folder and no active instances of it are loaded in the scene, the asset will not load.

they were not, rather in a separate stand-alone folder. I moved them to the "Resources" folder, but sadly that did not seem to help my situation. I will do that hence forward though, thanks for taking the time.

This should be a comment to the question.

I hear you! It's the main gotcha with concurrency. There are so many paths for the code to take (even in simple cases), and often most of them are valid, but there are certain outliers that will bite you.

PS: Have you migrated to the new version of Unity? Possibly some underlying change that exposed the issue?

@ZeroSumGames thank you for your response, my lack of knowledge leaves me wondering - where/how do I build this object list? I expected that it would be in the inspector when applying this script to a game object but I was mistaken, or perhaps I am doing it wrong? I will continue to try to figure it out any pushes in the right direction would be appreciated and thanks again for the answer.

You can create another script and attached it to all of the objects that you want to track. Like "TrackableObject.cs", and then in the Start Function you would add it to YourObjectList like indicated above.

Update: I created a objectPooler for Shots and Enemies. Replaced: if(col.tag == "enemy" && this.gameObject.tag == "playerShot") { col.SendMessage("TakeDamage", damage); Destroy(this.gameObject); with: if(col.tag == "enemy" && gameObject.tag == "playerShot") { col.gameObject.GetComponent<EnemyController>().TakeDamage(damage); gameObject.SetActive(false); } It does still happen but much less. I'm using Unity 5.5.4f1