Hi, my question is probably really stupid but I can’t seem to find a general rule for it or better, because sometimes it seem to work and sometimes not. Maybe someone can bring some enlightment to me ![]()
Here I add my variables in the beginning
public GameObject attack;
public GameObject defenseButton;
GameObject attackClone;
GameObject defenseButtonClone;
In the inspector I have dragged the prefabs for attack and defenseButton onto the slots in the script.
In the update function I instantiate the attack GameObjekt as affackClone (inside an if statement)
attackClone = Instantiate(attack);
attackClone.transform.SetParent(thisIsTheCanvas.transform, false);
attackClone.tag = "AttackButton";
In the same script a little further down I have a function called AttackButtonClicked which get’s called if the AttackButton gets clicked (On Click () of UI Button). Inside this script I want to destroy the attackClone. But in order to do that I have to define the variable attackClone again using FindwithTag.
public void AttackButtonClicked () {
attackClone = GameObject.FindWithTag ("AttackButton");
Destroy(attackClone);
ShowDefenseButton ();
Debug.Log ("Button Destoryed" + attackClone);
}
I just don’t know why I have to assign the variable again? Shouldn’t it work all over this script? If not why? Has it something to do with ActionButtonClicked being called by a UI Button? Maybe someone can explain this to me?
EDIT
Here’s teh fullcode after changing it from the suggestion from the first answer
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class SelectCountry : MonoBehaviour {
public bool attackFromCountry = false;
public bool attackThisCountry = false;
public bool buttonIsThere = false;
//GameObject attackingCountry;
int unitsInAttackingCountry;
//Canvas and Buttons
GameObject thisIsTheCanvas;
public Button attack;
public GameObject defenseButton;
public Button attackClone;
GameObject defenseButtonClone;
//Selected Countries to reset
GameObject firstSelectedCountry;
GameObject secondSelectedCountry;
void Start (){
thisIsTheCanvas = GameObject.Find("Canvas");
}
void Update() {
if (Input.GetMouseButtonDown(0)){
Debug.Log ("LeftClick");
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (hit.collider != null) {
// Select Country to attack from
if (attackFromCountry == false && hit.collider.tag == "Untagged"){
//attackingCountry = hit.collider.GetComponent<CountryBaseClass>();
Debug.Log ("wossers");
unitsInAttackingCountry = hit.collider.GetComponent<CountryBaseClass>().unitsCountry;
if (unitsInAttackingCountry >= 2){
hit.collider.tag = "isActiveAttacker";
attackFromCountry = true;
Debug.Log ("wow");
}
}
// Select Country you want to attack and make it possible to deselect country
else {
if (attackThisCountry == false && hit.collider.tag == "Untagged" && attackFromCountry == true){
hit.collider.tag = "isActiveDefender";
attackThisCountry = true;
Debug.Log ("super");
}
else {
if (attackFromCountry == true && hit.collider.tag == "isActiveAttacker" && attackThisCountry == false){
hit.collider.tag = "Untagged";
attackFromCountry = false;
}
if (attackThisCountry == true && hit.collider.tag == "isActiveDefender"){
hit.collider.tag = "Untagged";
attackThisCountry = false;
}
}
}
}
}
// Show Attack Button if a two countrys are selected
if (attackFromCountry == true && attackThisCountry == true && buttonIsThere == false) {
attackClone = Instantiate(attack);
attackClone.transform.SetParent(thisIsTheCanvas.transform, false);
attackClone.tag = "AttackButton";
buttonIsThere = true;
}
// Destroy attack Button if country is deselected
if (buttonIsThere == true && attackThisCountry == false) {
Destroy(attackClone);
Debug.Log ("Button Destoryed" + attackClone);
buttonIsThere = false;
}
}
// Check if attacked button was klicked and then disable it and show defend button
public void AttackButtonClicked () {
//attackClone = GameObject.FindWithTag ("AttackButton");
//Destroy(attackClone);
attackClone.enabled = false;
ShowDefenseButton ();
Debug.Log ("Button Destoryed" + attackClone);
}
public void ShowDefenseButton () {
defenseButtonClone = Instantiate(defenseButton);
defenseButtonClone.transform.SetParent(thisIsTheCanvas.transform, false);
Debug.Log ("Show Defense");
}
public void DefenseButtonClicked () {
defenseButtonClone = GameObject.FindWithTag ("DefenseButton");
Destroy(defenseButtonClone);
//reset
firstSelectedCountry = GameObject.FindWithTag ("isActiveAttacker");
firstSelectedCountry.tag = "Untagged";
secondSelectedCountry = GameObject.FindWithTag ("isActiveDefender");
secondSelectedCountry.tag = "Untagged";
}
}
More info please! It's not clear why you think you do have to assign it again. What happens if you don't? And do you know for sure that the object has been instantiated when the ActionButtonClicked gets called?
– Bonfire-BoyIf I don't assign it again, meaning "attackClone = GameObject.FindWithTag ("AttackButton");", then the Destroy(attackClone) doesn't work and the attackClone wont get destroyed. This is the output in the console: Button Destoryednull UnityEngine.Debug:Log(Object) The object it self gets instantiated in the update function and yes it get's instantiated. I can see the clone in the hierarchy. I thought that the variable attackClone should work in the whole script and not only in the update function.
– Peterwerd