I am just starting some do some scripting in Unity, and don't really know much of anything yet, so any and all help and advice will be appreciated. I am trying to use a UI to turn parts of a mesh on and off, (some are simple on or off, some are one turns on while another part turns off). I made a script that triggers on a button click, and it will turn the affected object off, but not turn it on again, and I get the error: NullReferenceException: Object reference not set to an instance of an object SwitchState.Togglestate () (at Assets/Scripts/SwitchState.cs:14) UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:166) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update() So I am missing something but not sure what exactly. The object in the scene has a prefab with the script attached, and the mesh is inside the prefab and has three parts (switch_low_height, SwitchOpen, SwitchClosed), The only other thing of note I can think of is the UI is in worldspace, and I have it included in the prefab. Prefab (with script component) -FBX --Main Model --Switch On --Switch Off -UI Canvas Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class SwitchState : MonoBehaviour { private bool toggle; private GameObject SWopen; public void Togglestate() { SWopen = GameObject.Find("SwitchOpen"); SWopen.SetActive(false); if (!toggle) { toggle = true; SWopen.SetActive(true); } else { toggle = false; SWopen.SetActive(false); } } } Any help is appreciated. Thanks
GameObject.Find() doesn't return inactive objects, so after you toggle it off the first time, it'll be null the second time. Store a reference instead of "Find"ing it every time.
Also, you should get rid of line 11 in the code sample you posted. I've had problems in the past when I tried to do SetActive(true) and SetActive(false) on the same GameObject in a single frame. I expected the last call to win, but it doesn't appear that's necessarily true. And in this case, you can be sure the initial call is unnecessary--all possible code paths after it call SetActive() anyway--so removing it should be harmless at worst, and might save you headaches later.
So I have just been using the GetComponent<Renderer>(); and that has worked for me rather than SetActive. and that works, but I am wondering if there is a easier way to do this, rather than trying to find each object name and making the script know about it, just stick a script on each object and then connect it to the UI respective UI button (I have maybe three hundred unique objects that need to toggle visiblity based on UI buttons. Can I make a generic script that can be attached to the mesh and turn the rendering on and off? Thanks.
There's definitely ways to automate or simplify this, but could you elaborate on your hierarchy structure? It's hard to imagine what buttons link to what scripts and whatnot without it.
If you've successfully written some code that can turn rendering on and off, then you should absolutely be able to put that code into a MonoBehaviour and attach it to your individual objects. The key question is probably: how does the code know when to turn rendering on and off? There are many possible answers to that, depending on how your game is organized and what you are trying to accomplish.
Hi thanks for continuing on this with me. I appreciate it. So, what I have is a FBX, it has a bunch of parts named appropriately within itself. I have a series of UI panels that you approach, and there are buttons you press to toggle the various mesh parts on and off (if it matters the UI panels are in world space). So at this point, I have, the FBX, with a flat hierarchy with the parts that turn off and on. I also have several UI panels with the buttons on them. Each of these panels control a specific group of the FBX parts. I have this script on each part in the FBX: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class ToggleVis : MonoBehaviour { public bool toggle; private Renderer rend; // Use this for initialization public void togglewire () { print(gameObject.name); rend = gameObject.GetComponent<Renderer>(); rend.enabled = true; if (!toggle) { toggle = true; rend.enabled = (true); print("turn On"); } else { toggle = false; rend.enabled = (false); print("turn off"); } } } And then the UI button OnClick() is calling the script. It's actually working OK now, with a couple of issues. Some objects are on by default, and some are off be default. The ones that are off default turn on on first click, the ones that are off by default have to be clicked three times to turn off. I am wondering if I need to do something with not having a Start() and Update() structure? Also, my debug commands seem to stop working after a couple clicks as well. I will also have to get the UI buttons to reflect the state they are in (changing color or swapping a texture on another button element) and I also want to get a 'reset' button to put each UI panel back to the initial state. But for now, I just want to get the button clicks to work like I want. Thanks, I am just muddling my way through this, but hoping to get a bit more understanding as I go along.
There is a built-in Unity UI object called a Toggle that is designed for making checkbox buttons and similar stuff. It may or may not be useful for you. Just like before, I recommend you delete the line that always sets "enabled = true" right before you decide whether you want it to be true or false. That line is doing nothing at best and might conceivably cause problems. The most likely reason for you needing to click multiple times in order to make something change is if your "toggle" variable is out of sync with the actual renderer, causing you to e.g. try to turn it off when it's already off. You could consider getting rid of the "toggle" variable entirely and just setting rend.enblaed = !rend.enabled;
Great thank you! MUCH cleaner. I changed the code and it seems to be working much better on first toggle. I had used another example code and it had the toggle variable. I now have this Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class ToggleVis : MonoBehaviour { private Renderer rend; // Use this for initialization public void togglewire () { print(gameObject.name); rend = gameObject.GetComponent<Renderer>(); rend.enabled = !rend.enabled; print("Toggled"); } } But I do get an odd error message when the script if first compiled "Unable to parse file Assets/Scripts/ToggleVis.cs.meta: [mapping values are not allowed in this context] at line 4" but it goes away and does not affect the script otherwise. And the print debugs also only work once. Not a huge deal, but I was wondering if that is something that I can change so that the debugs work?
Are you sure you don't have the console set to collapse so it stacks the print message? If togglewire is being called correctly, your prints are going to work.
If the error complaints about the .meta file, then it's probably not related to your source code at all. Unity uses *.meta files to keep track of your assets, so it's probably some kind of asset-management issue. I've never seen that particular error before, though.