I have a ui Button called "Attack Button". I have a script called HeroBattleController attached to my Gameobject. I have an attack button set as a serialized field and the attack button object dropped on the script in the editor.
[SerializeField]
private Button AttackButton;
public void SetButtonStatus(bool status) {
AttackButton.interactable=status;
}
Trying to access that gives me the error NullReferenceException: Object reference not set to an instance of an object
I thought putting the object in the editor would allow me to access it without having to "Find" the object. Can anyone point me in the right direction?
The error is on the line
AttackButton.interactable=status;
Complete HeroBattleController script
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class HeroBattleController: MonoBehaviour {
public static string SelectedHero;
[SerializeField]
private Button AttackButton;
public void SetButtonStatus(bool status) {
AttackButton.interactable=status;
}
public void HeroTouch() {
Debug.Log("Hero was touched: "+this.name);
SetButtonStatus(false);
}
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
}
}
HeroTouch is called via onClick from the heroprefab object.
Update: I had the herobattlecontroller script attached to two objects, the gameobject at the root of the scene and the heroprefab object. I removed it from the gameobject and only have it on the prefab. However now when I drag the attack button to the script section on the prefab it stays bold and when I run the game the attack button reference is gone. I can drag the button to the running object and it functions as I expected. I'm clearly missing something on the connection between the object hierarchy and where the objects are usable. Putting the hero battle controller script on the root gameobject only acts the same way meaning it's bold and when ran it's missing the link.
hierarchy during edit
hierarchy during runtime
Does HeroBattleController attached to a prefab? and does the AttackButton is on the same prefab ? if not then there is a problem.
Related
I am a beginner in Unity and I am currently making a simple game. I have a problem where I need to select an object automatically by script so I can click outside of it without clicking on it in the screen.
What I want to do is when I click the object another object or panel will show and it will be automatically be selected by script so I can click outside the panel and it will close or will setActive to false. I tried the Select() and it is not working for me. The two objects are originally an Image but I added a Button component so it can be selectable.
This is the script for the object that will show the Panel:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ShowPhone : MonoBehaviour, ISelectHandler
{
[SerializeField] Button phonePanel;
public void OnSelect(BaseEventData eventData)
{
phonePanel.gameObject.SetActive(true);
}
}
This is the script for the panel where I want to click outside to close it:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class HidePhoen : MonoBehaviour, IDeselectHandler
{
public Button phone;
private void OnEnable()
{
phone.Select();
}
public void OnDeselect(BaseEventData data)
{
Debug.Log("The Object will be setActive false when clicked outside the object");
}
}
You would use EventSystem.SetSelectedGameObject
EventSystem.current.SetSelectedGameObject(yourTargetGameObject);
Note though: Your panel would automatically be closed even if clicking any UI element within it (like e.g. InputField etc)
You can checkout the TMP_Dropdown source code and the way they implemented a "UI Blocker" which basically is an overlay over the entire screen except the drop-down popup and closes the pop-up when clicked on.
you could either replicate this or have a simpler version with a fully transparent background button behind your panel with onClick for closing the panel.
As alternative you could check RectTransformUtility.RectangleContainsScreenPoint
I have an MRTK Slate which comes with Pressable Button to close the slate.
When the close button is pressed, the Slate is disabled but not destroyed. This is because in the Events section>Interactable script> GameObject.SetActive() is set by default as shown in fig:
I want to destroy the slate after clicking close button.
I know I can do this by making a script, attaching the slate prefab to the script(or taking the parent of the button till I get slate as parent game object), calling the function, and using GameObject.Destroy().
But I want to understand how the GameObject dropdown in the Interactable script is getting populated :
I understand that the other dropdowns like Transform, Follow me toggle, Solverhandler, etc are displayed because they are attached to the Slate.
But how does the GameObject option is available? This seems a basic thing, but I would like to be sure about how it is coded.
And if it is possible to add my new action in the Gameobject dropdown, if so, how?
I spent time looking at the scripts of Interactable and others, but I am a beginner in C# and was not able to see where is the code which does this.
Any input will help me.
No you can't because Object.Destroy is static and in UnityEvent (which Interactable.OnClick uses) via the Inspector you can only select instance methods.
You at lest need a minimal component like
public class ObjectDestroyer : MonoBehaviour
{
public void DestroyObject()
{
Destroy(this.gameObject);
}
}
or if you don't want to do it via the Inspector but automatically
public class ObjectDestroyer : MonoBehaviour
{
// store the itneractable reference
[Tootltip("If not provided uses the Interactable on this GameObject")]
[SerialzieField] private Interactable _interactable;
// Optionally provide a different target, otherwise destroys the Interactable
[Tooltip("If not provided destroys the GameObject of the Interactable")]
[SerializeField] private GameObject _targetToDestroy;
private void Awake()
{
// use get component as fallback if no Interactable was provided
if(!_interactable) _interactable = GetComponent<Interactable>();
// attach the callback on runtime (won't appear in the inspector)
_interactable.OnClick.AddListener(DestroyObject);
}
private void DestroyObject()
{
// destroy the object of the interactable if no target was provided
if(!_targetToDestroy) _targetToDestroy = _interactable.gameObject;
Destroy(_targetToDestroy);
}
}
I have an event manager in Unity which can handle events sent from scene components. The classes are all a bit abstracted from the standard Unity components. I am successfully able to fire an event from a button, and have that button's parent listen to the event (the onEventReceived you see below).
Using the Unity Debugger extension for VSCode, I can log the sender. If I type sender.gameObject and it returns a correct reference to the GameObject the sender is attached to. However, I cannot reference sender.gameObject within the OnEventReceived function itself - because it is of type ISceneComponent I suppose. How do I get a reference to the game object of the clicked sender?
I have run into your issue myself before.
The fix is quite simple.
Assuming that you created the interface ISceneComponent, add the gameObject field to your Interface
public interface ISceneComponent
{
GameObject gameObject { get; set; }
// ...
}
If the object implementing ISceneComponent also is a monobehaviour, gameObject will automatically be filled and you can reference it easily.
I'm creating custom layout group and I want to control RectTransform on the child Objects. I want to lock some fields on child's RectTransform like when using canvas or Unity's Horizontal or Vertical group so that it cannot be modified.
I need the same effect. You can see this message on top of child's RectTransform : Some values driven by HorizontalLayoutGroup
I find a halfway :
Adding [ExecuteInEditMode] then:
public void Update()
{
#if UNITY_EDITOR
if (!Application.isPlaying)
{
/* Todo => update child's positions here. */
}
#endif
}
Any other idea?
This is done with the DrivenRectTransformTracker API.
From the doc:
Driving a RectTransform means that the values of the driven
RectTransform are controlled by that component. These driven values
cannot be edited in the Inspector (they are shown as disabled). They
also won't be saved when saving a scene, which prevents undesired
scene file changes.
Whenever the component is changing values of driven RectTransforms, it
should first call the Clear method and then use the Add method to add
all RectTransforms it is driving. The Clear method should also be
called in the OnDisable callback of the component.
No example from the doc but below is how to use it:
public RectTransform targetRC;
UnityEngine.Object driver;
void Start()
{
DrivenRectTransformTracker dt = new DrivenRectTransformTracker();
dt.Clear();
//Object to drive the transform
driver = this;
dt.Add(driver, targetRC, DrivenTransformProperties.All);
}
The RectTransform linked to the targetRC variable will now be locked and cannot be modified from the Editor. It should now say something like "Some values are driven by another object". You can use DrivenTransformProperties to specify which variables to lock.
This is what it looks like after executing this code:
I've been struggling with what appears to be a simple task for the past few hours. I have been successfully able to turn a GameObject's guiTexture on an off using GameObject.guiTexture.enabled as I have read in other similar questions. I do this in a script that is attached to the GameObject I wish to toggle.
The problem arises when I try to preform this same command from another script that is attached to an empty game object in my scene. When I interact with the button, simply nothing happens.
To give this some context, I am trying to create functionality to switch menus on the screen. When a user taps a certain UI button, I want to call unloadMenu() so I can wipe everything off the screen and draw the textures that belong to the appropriate menu.
//When the user lifts their finger
void OnTouchEnded ()
{
switch (this.gameObject.name)
{
//If they have selected the quiz button
case "QuizButton":
//this.guiTexture.enabled = false;
//Call the unloadMenu function which will clear the GUITextures
menuMgr.unloadMenu();
break;
The commented out section is what works fine. But now, I wish to perform this code in a function that is located in another script - menuMgr.unloadMenu()
This is what the MenuManager script looks like:
public GameObject quizButton;
void unloadMenu()
{
quizButton.guiTexture.enabled = false;
}
I have ensured to drag the quizButton gameobject from my scene into the inspector and connect it properly with the variable I have created yet nothing happens when this function is called. Does anyone have any idea why? Thanks in advanced.
How you declare MenuManager creates a new script while the reference to quizButton is in your MenuManager GameObject. That is why your menuMgr.UnloadMenu didn't do anything.
I have created something similar to yours and I found it worked fine :
Quizbutton Script:
public GameObject menuMgr; // reference to the empty game object (menu manager)
void OnMouseDown() // In your case, this is OnTouchEnded
{
// get the script of menu manager here
MenuMgrScript menuscript = menuMgr.GetComponent<MenuMgrScript>();
// and execute the function you want
menuscript.UnloadMenu();
}
Menu Manager Script:
// this script is exactly the same as yours, nothing's wrong here
// oh except add public to function UnloadMenu, so it can be called
// in the quizbutton script
public GameObject quizButton;
public void UnloadMenu() {
quizButton.guiTexture.enabled = false;
}