- Home /
How can I write: if this method (X) is activated by another specific method (Y) do this (z)
In this example x is CloseBuildingTree and I have a sound i want to play when it closes but it also closes when Y 'buildtower' is activated and I have another sound for that situation.
How can I write that 'tree close sound' isn't logged when closebuildingtree is activated by buildtower?
private void CloseBuildingTree()
{
if (activeBuildingTree != null)
{
Destroy(activeBuildingTree.gameObject);
// Enable tower raycast
bodyCollider.enabled = true;
Debug.Log("tree close sound");
}
}
/// <summary>
/// Builds the tower.
/// </summary>
/// <param name="towerPrefab">Tower prefab.</param>
public void BuildTower(GameObject towerPrefab)
{
// Close active building tree
CloseBuildingTree();
Price price = towerPrefab.GetComponent<Price>();
// If anough gold
if (uiManager.SpendGold(price.price) == true)
{
// Create new tower and place it on same position
GameObject newTower = Instantiate<GameObject>(towerPrefab, transform.parent);
newTower.transform.position = transform.position;
newTower.transform.rotation = transform.rotation;
// Destroy old tower
Destroy(gameObject);
}
}
So it appears that when I build a tower it runs both the build tower to close method and the click off one bellow:
private void UserClick(GameObject obj, string param)
{
if (obj == gameObject) // This tower is clicked
{
// Show attack range
ShowRange(true);
if (activeBuildingTree == null)
{
// Open building tree if it is not
OpenBuildingTree();
}
}
else // Other click
{
// Hide attack range
ShowRange(false);
// Close active building tree
CloseBuildingTree(true);
}
I tried something like this but I'm not sure how to write it, any tips?
https://gist.github.com/anonymous/85bb165682e1cee9a65fdd4c4b889686
Answer by Hellium · Jun 26, 2017 at 04:38 PM
private void CloseBuildingTree( bool calledByBuildTower )
{
if (activeBuildingTree != null)
{
Destroy(activeBuildingTree.gameObject);
// Enable tower raycast
bodyCollider.enabled = true;
if( calledByBuildTower )
Debug.Log("tree close sound");
}
private void CloseBuildingTree()
{
CloseBuildingTree( false ) ;
}
public void BuildTower(GameObject towerPrefab)
{
// Close active building tree
CloseBuildingTree( true );
Price price = towerPrefab.GetComponent<Price>();
// If anough gold
if (uiManager.SpendGold(price.price) == true)
{
// Create new tower and place it on same position
GameObject newTower = Instantiate<GameObject>(towerPrefab, transform.parent);
newTower.transform.position = transform.position;
newTower.transform.rotation = transform.rotation;
// Destroy old tower
Destroy(gameObject);
}
}
Hey thanks, thats in the right direction to where I want to go but it's doing what I want in reverse. I want the debug to not be called when 'buildtower' activates the method. how can I change it?
Simply change the condition inside the CloseBuildingTree
function :
if( !calledByBuildTower )
Debug.Log("tree close sound");
Sorry for another comment but adding if( !calledByBuildTower ) Debug.Log("tree close sound");
gives me the debug message both ways again. any ideas?
You should check the Trace in the console to find which function called the CloseBuildingTree
function.
I've recorded it to make it a bit easier to understand I hope https://streamable.com/fmart
So using what you said it appears that when I build a tower it activates both the build tower method and the 'click off to close' method. That being the case is it possible to say when tower build and tower click off methods are activated with the same click, don't play a sound in tower click off?