Unable to destroy instantiated prefab during OnMouseExit,Unable to Destroy previously instantiated object OnMouseExit()
Hi,
Just want to say that whilst this is my first post, and I've also tried to find the solution by searching the community, I can't fix this issue I'm having.
The basic behaviour I'd like to happen is:
User clicks button (got this working)
When the user hovers over another specific entity it turns green and shows a prefab on top of their current area (got this working)
When the user leaves that grid point, it should make turn the block back to the original colour (got this working)
as well as point 3, it should also remove the previously Instantiated prefab (this is what I'm struggling with)
Here's my code for the hovering that's working fine and works:
private GameObject temporaryPrefab;
private void OnMouseOver()
{
if (WorldManager.wm.placementPending)
{
Debug.Log("Showing ghost placement...");
GetComponent<SpriteRenderer>().color = Color.green;
temporaryPrefab = Instantiate(WorldManager.wm.currentlyPlacing.item.Prefab, gameObject.transform.position, Quaternion.identity);
}
}
Here's my code for when the mouse exits that area:
private void OnMouseExit()
{
if (WorldManager.wm.placementPending)
{
Debug.Log("Destroying previous placements...");
GetComponent<SpriteRenderer>().color = Color.white;
Destroy(temporaryPrefab); // This isn't working!
}
}
I should note that these two methods are in the same class and can both access the "temporaryPrefab" variable. The GameObject in "temporaryPrefab" does not get Destroyed as I want it too.
In trying to get this to work I've done the following:
Used Destroy(temporaryPrefab, 0f)
Stored the created prefabs in a list, and then looped through the list on exit (in case more than 1 was created?)
Any help on this would be appreciated. If you need any more info let me know!
,Hi,
Just want to say that whilst this is my first post, and I've also tried to find the solution by searching the community, I can't fix this issue I'm having.
The basic behaviour I'd like to happen is:
User clicks button (got this working)
When the user hovers over another specific entity it turns green and shows a prefab on top of their current area (got this working)
When the user leaves that particular grid point, it should make turn the block back to the original colour (got this working)
as well as point 3, it should also remove the previously Instantiated prefab (this is what I'm struggling with)
Here's my code for the hovering that's working fine and works:
private GameObject temporaryPrefab;
private void OnMouseOver()
{
if (WorldManager.wm.placementPending)
{
Debug.Log("Showing ghost placement...");
GetComponent<SpriteRenderer>().color = Color.green;
temporaryPrefab = Instantiate(WorldManager.wm.currentlyPlacing.item.Prefab, gameObject.transform.position, Quaternion.identity);
}
}
Here's my code for when the mouse exits that area:
private void OnMouseExit()
{
if (WorldManager.wm.placementPending)
{
Debug.Log("Destroying previous placements...");
GetComponent<SpriteRenderer>().color = Color.white;
Destroy(temporaryPrefab); // This isn't working!
}
}
I should note that these two methods are in the same class, and can both access the "temporaryPrefab" variable. The GameObject in "temporaryPrefab" does not get Destroyed as I want it too.
In trying to get this to work I've done the following:
Used Destroy(temporaryPrefab, 0f)
Stored the created prefabs in a list, and then looped through the list on exit (in case more than 1 was created?)
Any help on this would be appreciated. If you need anymore info let me know!