- Home /
The question is answered, right answer was accepted
SetActive doesn't work from .exe
I have a little program that asks multiple choice trivia questions. I want to be able to display and hide those questions and answers. It works in Unity... but not when compiled.
I have a GameController that holds a reference to a QuestionAnswerGroup GameObject that I want to sometimes be active and sometimes not be active. This works great when I am running the game in Unity, but when I build it and compile to an exe and run the game from there, things work totally differently. It never reactivates the group when it should. It will go from active to inactive but never back the other way. Only in the compiled code though. Like I said, when debugging in Unity it toggles on and off just great.
Does anyone have any idea what's happening here?
Here's a bit of my code:
In controller:
public void GetAndDisplayNewTriviaQuestion(string color)
{
var questionDisplay = new QuestionDisplay();
_currentQuestion = questionDisplay.GetNewQuestion(color, _qdb);
ActivateQuestionAnswerGroup(true);
questionDisplay.DisplayQuestion(_currentQuestion);
}
public void ActivateQuestionAnswerGroup(bool setActive)
{
_questionAnswerGroup.SetActive(setActive);
}
In a different script attached to a Button, call to deactivate (answerSelected is called OnClick):
public void AnswerSelected()
{
var rc = FindObjectOfType<RuleController>();
rc.CheckAnswer(this.GetComponentInChildren<Text>().text);
rc.ActivateQuestionAnswerGroup(false);
}
Oh dear, the code formatting did not keep at all... so sorry.
Are you sure you copied the code correctly? Because
FindObjectOfType();
is not a valid method call. It won't even compile / build. It's actually better not to use it at all, Find by Type is very slow.
Yeah, it seems anything inside of angled brackets didn't make it into the code I pasted. It should have the RuleController type specified. FindObjectOfType<'RuleController'>() This program is the first thing I've written using Unity, so most of what I'm using (such as FindObjectOfType) are from suggestions I found online. Is there another function you'd recommend for locating GameObjects?
Check Player log for errors (like UnassignedReferenceException): https://docs.unity3d.com/$$anonymous$$anual/LogFiles.html Put Debug.Log into GetAndDisplayNewTriviaQuestion and ActivateQuestionAnswerGroup to check each variable for null. Are you using Find elsewhere? It's only returns active objects. Also deactivating object will stop it's Update's and coroutines. And if you deactivate parent then you won't be able to activate child. If direct assignment is not possible then the best option from Find is FindWithTag. Alternatively, you can use a singleton.
Answer by dwittsac · Aug 04, 2020 at 04:36 PM
Thank you to @Elango and @davidcox70 The issue was in fact that the CSV file couldn't be located because: "When Unity compiles, it only includes the files that it needs to build scenes plus everything it finds in the Assets/Resources folder. So if your CSV file is not in Assets/Resources, it will not make it to the build and that might be were your problem is ultimately to be found."
Resolution was to move the file to Assets/Resources and use Resources.Load()
Thanks!