- Home /
Removing and Adding gameobjects to an array upon collision
Hello. I'm attempting to do something that might be a bit difficult. I'm making a 2D game where the player has 2 child objects attached to it. They both have a collider attached to them and are positioned to the left and to the right of the player, outside the camera.
The right one enables the MeshRendereron the floor(which is made of boxes) and the left one disables the MeshRenderer. I'm attempting to optimize this script so it also destroys these objects but I can't. because the player adds all of them to an array upon starting the game.
In the Start() I assign the PlayerScript to a variable called playerScript
private PlayerScript playerScript;
void Start()
{
playerScript = GetComponent<PlayerScript>();
}
But I'm unable to reach the array and modify it for some reason.
if(other.CompareTag("Floor"))
{
if (renderObject == true)
{
//Add it to the array
}
else
{
//Remove it from the array
}
}
if I start typing playerscript.RedFloorArray nothing show up.
Thanks for reading
Is the array public in the PlayerScript? If not you will have to make it public or write public functions to set/get/modify the array
totally forgot to make it public.. How do I then modify the array? Will I have to reset it and set it again? I'm guessing this would cause lag if there's a lot of boxes.
You could try using a List ins$$anonymous$$d. You can then add and remove using things like: mylist.Add(newItem); mylist.Remove(deadItem);
List isn't the best option for a frequently-size-modified collection; HashSet is.
Oh ok, that seems easy enough. But I can't add gameobjects with a tag to it.
RedFloorList.Add(FindGameObjectsWithTag("RedColoredFloor"));
Any other way?
Answer by giudark · Jan 23, 2014 at 01:59 PM
In PlayerScript you can simply create a function like this one you are trying to wrote outside (i assume that the RedFloorArray is an array of GameObject):
public void AddFloorArray(){
GameObject child = GameObject.FindGameObjectsWithTag("Floor") // or you can pass it as parameter
RedFloorArray.Add(child);
}
public void RemoveFloorArray(){
for(int i = 0; i<RedFloorArray.length; i++){
obj = RedFloorArray[i];
if(obj.CompareTag("Floor")){
RedFloorArray.RemoveAt(i);
}
//maybe --> Destroy(obj);
}
}
public void RemoveFloorArray(GameObject child){
for(int i = 0; i<RedFloorArray.length; i++){
obj = RedFloorArray[i];
if(obj.name.Equals(child.name)){
RedFloorArray.RemoveAt(i);
}
//maybe --> Destroy(obj);
}
}
So you can call this function outside without problem:
OnCollisionEnter(Collision col){
playerScript.RemoveFloorArray(col.gameObject);
}
Is that a list? the RedFloorArray variable? Edit: All the floor blocks has the same name... so :/
in this example it is an Array (class), but it can be easly a list. You can modify this:
if(obj.name.Equals(child.name))
in this:
if(obj.tag.Equals(child.tag))
or implementing some other way to differentiate what you want to delete (for exaple if you know the index of the block):
public void RemoveFloorArray(int index){
GameObject obj = RedFloorArray[index];
RedFloorArray.RemoveAt(index);
//maybe --> Destroy(obj );
}
I learnt that each item in the scene has a unique ID that I can find with .GetInstanceID()
but when I call the RemoveFloorArray(); I get this error:
NullReferenceException: Object reference not set to an instance of an object
RenderFloor.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/RenderFloor.cs:22)
This is the code that calls it:
playerScript.RemoveFloorArray(other.gameObject);
And the Function modified a bit
public void RemoveFloorArray(GameObject newObj)
{
Debug.Log("RemoveFloorArray Called");
if(newObj.CompareTag("RedColoredFloor"))
{
foreach(GameObject redFloorObj in RedFloorList)
{
if(redFloorObj.GetInstanceID().Equals(newObj.GetInstanceID()));
{
RedFloorList.Remove(redFloorObj);
}
}
}
the debug.log line doesn't get called, so I'm guessing something went wrong in calling it...
EDIT: Used this code ins$$anonymous$$d playerScript = transform.parent.gameObject.GetComponent();
which now calls the function. but the list is being accessed somewhere else in the playerscript and says the instance of the object was deleted...
NV$$anonymous$$. Changed it to disable the object ins$$anonymous$$d of destroying it. Works now. Thanks ;)
The Array class is only available in Javascript. http://docs.unity3d.com/ScriptReference/Array.html
Does this mean i cant use the RemoveAt method if im program$$anonymous$$g in C#?
RemoveAt() isnt showing in the autocomplete and is throwing a "does not contain a definition for 'RemoveAt'.
Your answer
Follow this Question
Related Questions
[C#] how do I get the name of the object under the player? 1 Answer
Multiple Cars not working 1 Answer
Attacking more than one enemy with same tag, but unity only allows one enemy at a time? 2 Answers
Only instantiate GameObjects with a certain tag from an array 1 Answer
Question about FindObjectsOfType 1 Answer