The function activateTab()
is being called on button press with a parameter being passed with it according to which a GameObject’s padding is supposed to be changed.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class tabManager : MonoBehaviour
{
private GameObject _body;
void Start()
{
// Instantiate GameObject
_body = GameObject.FindGameObjectWithTag("body");
}
void Update()
{
}
public void activateTab(int tabNo)
{
// Change padding of the Component based on the parameter tabNo's value
_body.GetComponent<UnityEngine.UI.HorizontalLayoutGroup>().padding.left = tabNo * -Screen.width;
}
}
The padding will only be reflected in the Scene if the change is being made in Update()
. In other words, changes to GameObjects will only be reflected in the scene if they are made in Update()
function. I am aware that I can make this work in the following way:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class tabManager : MonoBehaviour
{
private GameObject _body;
private int _tabNo;
private bool _updateGameObject = false;
void Start()
{
// Instantiate GameObject
_body = GameObject.FindGameObjectWithTag("body");
}
void Update()
{
if (_updateGameObject)
{
_updateGameObject = false;
// Change padding of the Component based on the parameter tabNo's value
_body.GetComponent<UnityEngine.UI.HorizontalLayoutGroup>().padding.left = _tabNo * -Screen.width;
}
}
public void activateTab(int tabNo)
{
// Have to update the gameObject w.r.t tabNo
_updateGameObject = true;
_tabNo = tabNo;
}
}
This is updating the GameObject in the Update()
function. But I have multiple functions which accept parameters and modify GameObjects in a similar way so making a pair of bool variable and a variable to store data incoming from respective functions mustn’t be the only way to do it. Is there a better way to do so?
PS. I’m new to Unity and I apologise if this is a very basic doubt but I had no luck googling for a solution to this problem