After the player button is clicked,when I touch on the screen, player gameobject is instantiated.I am adding the instantiated Gameobject into a list along with and ‘id’ . Code is given below.
var PlayerGo = Instantiate(player, fingerPositions[0], Quaternion.identity);
int touchCounts = Input.GetTouch(0).tapCount;
for (int i = 1; i <= touchCounts;i++)
{
// 'i' is the id being added to GetAllPlayers.
GetAllPlayers.Add(new PlayerHandler(i, PlayerGo));
}
Consider I tapped on the screen 4 times then 4 Player gameobjects will be instantiated.Next I turn off the player button and when I double tap on the screen and on top of the player gameobject(Recently instantiated ones).I want to delete that game object from scene and clear the same gameobject from the list.The code for double tapping is given below.
if (hit.collider.tag == "Playersone")
{
Debug.Log("Inside hit");
if (Input.GetTouch(0).phase == TouchPhase.Began && once == true)
{
touchTimeEnd = Time.time;
once = false;
float timediff = touchTimeEnd - touchTimeStart;
if (timediff < 0.3f)
{
//Delete the Gameobject from the list
}
}
if (Input.GetTouch(0).phase == TouchPhase.Began && once == false)
{
touchTimeStart = Time.time;
once = true;
}
}
The raycast hit returns you the gameobject in hit.collider.gameObject. Now that you have the gameObject you should be able to simply iterate over your list of players and check for each element whether the gameobject in that playerhandle is the same (.Equals()) as the hit gameObject. If that’s the case, delete that list entry and afterwards Destroy(hit.collider.gameObject) and you should be done.
Also, “GetAllPlayers” seems to be a list, but the name implies it’s a method. Method names should be commands or instructions like “DoX()” while variables should describe their content like “shoppingCart” or “playerList”.
You should just be able to compare gameObject1 == gameObject2, or use GameObject.ReferenceEquals( gameObject1, gameObject2), or if you want to just compare the ID’s of the gameobjects you are compare gameObject1.GetInstanceID() == gameObject2.GetInstanceID(). Either of those should return true when the gameobjects are equal and false otherwise.
for (int i = 0; i < getAllPlayers.Count;i++)
{
if (hit.collider.gameObject == getAllPlayers[i])
{
Debug.Log("Same Gameobject " +getAllPlayers[i]);
}
}
I have checked like this and when I double tapped on many instantiated objects.The condition came true when i clicked on the last instantiated player.Actually the condition should run for all double tapping all players.
Is getAllPlayers a list of GameObjects or PlayerHandlers? Because what you are adding to it in your first code sniplet in line 7 is a new instance of “PlayerHandler”. Since i cant find any information about a PlayerHandler type i’m assuming it’s your own type. So unless i’m missing something you would be comparing PlayerHandlers with GameObjects, right? Which should always be false…, so if anything he one time it returns true is confusing to me.
This works for arrays, but you can’t modify a List when iterating through it.
However, as Yoreki has pointed out, you know what GameObject you are to delete, so you can simply create a function like:
void Deleteobject()
{
RaycastHit hit;
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Destroy(hit.transform.gameObject);
RemoveFromList(hit.transform.gameObject);
}
}
}
A bit off topic, but this is wrong. You can modify a list while iterating it. It is only not possible to modify lists while iterating them in a foreach loop. A while or for loop causes no problems.
Example: Code
List<int> vals = new List<int>();
vals.Add(0);
vals.Add(1);
vals.Add(2);
foreach (int i in vals)
{
if (i == 1)
{
// This is not possible and throws a compiler error
//vals.Add(2);
}
}
for (int i = 0; i < vals.Count; i++)
{
if (i == 1)
{
// This however works
vals.Add(2);
}
}
// Will print 4 since we added an element
Debug.Log(vals.Count);
Usually when using raycast technique you don’t want to know which gameobject you hit or compare it with other objects to determine what to do. I suspect you want to know wich object is hit because you want to do one or another thing depending on the hit object? In that case let object choose what to do itself. That object already nows what to do when clicked.
I have never made an App with Unity before, so i cant really help on the touchy’bits either. I agree that, if the deletion works at all, then the deletion code itself should not be the problem. Thus we need to figure out where the problem actually lies. I’d start by printing useful information using Debug.Log(), to, for example, show when you entered a specific if-statement (for example the one with the deletion code inside). That way you can check at runtime whether the code block is actually called everytime you double-tap. If it is, then the problem is inside that code-block (the deletion code), if it is not, then the problem lies outside of it - for example a too short time window in which 2 taps are registered as a double-tap, causing the code-block to not be executed.
That said, there are (free) assets to help with managing touch stuff, including gestures (like double tapping) and so on.
Touchscript supposedly works great. I cant really help with how to use it tho. The only reason i know about Touchscript is because it also manages TUIO protocol communication, which is what i once used it for in a small uni prototype.