Hi Guys,
I can’t get my button to be only non interactable but still visible, I can only destroy it. Can someone point me in the right direction? I know that the code below calls destroy
but I tried various other things but argh
Cheers!
public class Healer : MonoBehaviour
{
public GameObject player1Magic;
public GameObject player2Magic;
GameObject obj;
public void Start()
{
obj = GameObject.FindGameObjectWithTag("GameMaster");
}
void Update()
{
if (obj.GetComponent<GameMaster>().playerTurn == 1)
{
player1Magic.SetActive(true);
player2Magic.SetActive(false);
}
else
{
player1Magic.SetActive(false);
player2Magic.SetActive(true);
}
}
public void giveGold()
{
if (obj.GetComponent<GameMaster>().playerTurn == 1)
{
obj.GetComponent<GameMaster>().player1Gold += 100;
Destroy(this.gameObject);
}
else
{
if (obj.GetComponent<GameMaster>().playerTurn == 2)
{
obj.GetComponent<GameMaster>().player2Gold += 100;
Destroy(this.gameObject);
}
}
}
using UnityEngine.UI;
Button myButton = whateverObject.GetComponent<Button>();
myButton.interactable = false;
1 Like
Thank you, but I should have posted the whole thing I guess as I cant get my head around it 
So what it should do is
Switch the Canvas so when its player 1 turn it shows the Buttons for P1 and vice versa for P2 → Works
Have mechanics to give the player whos turn it is more gold / time → works
Now with the code below, only the “gold magic = false” for P1 works. All other buttons - time magic for P1 and both buttons for P2 - are not going inactive. the buttons are set in the inspector, so public Button goldMagic2 for example has the button on it.
Script says for all of them “object reference not set to an instance of an object”, and this seems to be a core principle I do not understand yet. Would be great if you can help so I can use this in the future
as for me it is exactly the same as the working P1 Gold button…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Healer : MonoBehaviour
{
public GameObject player1Magic;
public GameObject player2Magic;
public Button goldMagic;
public Button timeMagic;
public Button goldMagic2;
public Button timeMagic2;
GameObject obj;
public void Start()
{
obj = GameObject.FindGameObjectWithTag("GameMaster");
}
void Update()
{
if (obj.GetComponent<GameMaster>().playerTurn == 1)
{
player1Magic.SetActive(true);
player2Magic.SetActive(false);
}
else
{
player1Magic.SetActive(false);
player2Magic.SetActive(true);
}
}
public void giveGold()
{
if (obj.GetComponent<GameMaster>().playerTurn == 1)
{
obj.GetComponent<GameMaster>().player1Gold += 100;
goldMagic.interactable = false;
}
else
{
if (obj.GetComponent<GameMaster>().playerTurn == 2)
{
obj.GetComponent<GameMaster>().player2Gold += 100;
goldMagic2.interactable = false;
}
}
}
public void giveTime()
{
if (obj.GetComponent<GameMaster>().playerTurn == 1)
{
obj.GetComponent<GameMaster>().currentTime += 20;
timeMagic.interactable = false;
}
else
{
if (obj.GetComponent<GameMaster>().playerTurn == 2)
{
obj.GetComponent<GameMaster>().currentTime += 20;
timeMagic2.interactable = false;
}
}
}
}
The error object reference not set to an instance of an object means you’re trying to access something that doesn’t exist, like a reference being null. You’ll need to debug and figure out which object isn’t set, whether it’s your obj, GameMaster component, or one of the buttons.
Thank you, basically it was me being an idiot ^^