Reputation: 165
I put the all GameObjects in scene under a list. the following is function used in the shelf.cs to delete the gameobjects. I assign the function to a button, so that it takes input value and find which Gameobject has lesser value than input. Then delete. The problem is whenever I click the button in the game preview, it won't delete the gameobjects. There are no warnings. I debug whether it receives all input, and it did. just not deleting the GameObjects.
Why?
public void Process(){
int user_apple,user_lemon,user_watermelon;
user_apple = int.Parse (input_apple.text);
user_lemon = int.Parse (input_lemon.text);
user_watermelon = int.Parse (input_watermelon.text);
Debug.Log (user_apple+" "+user_lemon+" "+user_watermelon);
for(int i = players.Count - 1; i >= 0; i--)
{ if(players[i].name == "Lemon")
{
if(players[i].GetComponent<Apple>().weight <= user_apple)
{ Debug.Log ("wat u want");
Destroy(players[i]);
}players.RemoveAt(i);
}
}
}
if i were to put it like this,
public void Process(){
int user_apple,user_lemon,user_watermelon;
user_apple = int.Parse (input_apple.text);
user_lemon = int.Parse (input_lemon.text);
user_watermelon = int.Parse (input_watermelon.text);
Debug.Log (user_apple+" "+user_lemon+" "+user_watermelon);
if(players[2].GetComponent<Lemon>().weight <= user_apple)
{ Destroy(players[2]);
players.RemoveAt(2);
}
}
it will have errors like below
FormatException: Input string was not in the correct format
System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Int32.cs:629)
Basket.Process () (at Assets/scripts/Basket.cs:77)
UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/UnityEvent.cs:110)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/UnityEvent.cs:575)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/UnityEvent.cs:717)
UnityEngine.Events.UnityEvent.Invoke () (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/UnityEvent_0.cs:53)
UnityEngine.UI.Button.Press () (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/Button.cs:36)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/Button.cs:45)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/EventSystem/ExecuteEvents.cs:52)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/EventSystem/ExecuteEvents.cs:269)
UnityEngine.EventSystems.EventSystem:Update()
Upvotes: 1
Views: 142
Reputation: 1075
This may not be the right solution try calling a co-routine from the Process Function and yield the loop so it runs everyframe.
Upvotes: 1
Reputation: 2989
Just to recap what Joe Blow said: there is no reason why this for loop should not work since it is totally independant of frames/time.
This little example works perfectly fine:
The script ScriptHolder
is assigned to an empty gameobject (see screen below) and looks like this:
using UnityEngine;
using System.Collections.Generic;
public class ScriptHolder: MonoBehaviour
{
public List<GameObject> theGameObjects;
public void TheButtonFunction(int weight)
{
for(int i = theGameObjects.Count - 1; i >= 0; i--)
{
if(theGameObjects[i].GetComponent<TheObject>().objectWeight <= weight)
{
Destroy(theGameObjects[i]);
theGameObjects.RemoveAt(i);
}
}
}
}
All gameobjects were dragged into the public list.
The script for the gameobjects looks like this:
using UnityEngine;
using System.Collections;
public class TheObject : MonoBehaviour
{
public string objectName = "";
public int objectWeight = 0;
}
All GameObject_A
are named A
and have a weight of 10
(done in the inspector), GameObject_B
are B
with a weight of 15
and GameObject_C
are C
with a weight of 20
. Columns are A to C from left to right.
The button calls the function from above with a value of 15
.
When I click the button, the left and the middle columns of gameobjects are deleted from the scene and from the list - no problem whatsoever.
Upvotes: 1