#Destroy Transform for Justin-Dude

1 messages · Page 1 of 1 (latest)

gentle scroll
#

So, you tried making it a List<Transform> and manually removing the element from the list, and it's still there?

#

@olive hollow

olive hollow
#

yeap

#

wait let me see if I can explain this better

gentle scroll
#

Sure thing (though I don't have a lot of time before I need to go)

olive hollow
#

so I made the list, if I destroy the second element first it works good, if I destroy the first it get problem

#
        print("checking for weapons stored");
        for (int i = 0; i <=EquipedWeapons.Count ; i++)
        {
            
            if(EquipedWeapons[i].gameObject!=null){
                print(EquipedWeapons[i].transform);
                return EquipedWeapons[i].transform;
            }

           
        }
        return null;
    }```
gentle scroll
#

When you Remove the first element from the List, the second element then becomes the first element in the List (that's a difference between a List and array).

#

So, if EquippedWeapons is a List, EquippedWeapons[0] will always be the first non-removed element.

olive hollow
#

so I need to destroy the element, push the second now first to the second as well and make a empty first?

gentle scroll
#

I don't think so.

#

Can you paste your code that adds weapons to the list?

olive hollow
#

EquipedWeapons[0]=Instantiate(_slot.ItemObject.prefab, WeaponPositionSlots[0]).transform;

#

I am saying that the list[0] is an instantiation of an object

#

I got close identical names could make some confusion

#

but even so, when I destroy an list element the second should be the first now, its not even happening like that

#

I got 2 items, 1] pistol 2] rifle

gentle scroll
#

Instead, since EquippedWeapons is a list, you should use:

EquipedWeapons.Add( Instantiate(_slot.ItemObject.prefab, WeaponPositionSlots[0]).transform );
olive hollow
#

but I need a certen element to be in slot 0 and the other in slot 1

gentle scroll
#

OH, I think I see what you're trying to do. I thought you wanted to have the 1st weapon become the 0th weapon when the old 0th one was destroyed.

#

In that case, just set it to null manually like this:

#
Transform[] EquippedWeapons;
…
…
…
// in your other code
Destroy(EquippedWeapons[0].gameObject);
EquippedWeapons[0] = null;
#

If you try to check the field of a null value, you will get a NullReferenceException. So in your code snippet, instead of
if(EquipedWeapons[i].gameObject!=null){
you would need
if(EquipedWeapons[i] != null){

olive hollow
#

wait

gentle scroll
#

And because EquippedWeapon[i] IS a Transform, you can just print and return EquippedWeapon[i]

#

I'm recommending going back to the array if you don't want the elements to move around.

olive hollow
#

Destroy(EquippedWeapons[0].gameObject); is givim me null

#

null ref

#

affter I call it = null

gentle scroll
#

Oh right. Swap the lines so that you destroy the gameObject before setting EquippedWeapon[0] = null;

#

The destroy actually happens in cleanup before the next frame, but the setting to null happens immediately.

#

I fixed my code snippet

olive hollow
#

WAIT MAN SORRY DUDE HELP ME OUT HERE

#

caps

#

I had a problem before that, and this was the main culprit, with array was doing the same shit

#
    public bool isWeaponListEmpty(){

        
        for (int i = 0; i <=EquipedWeapons.Count ; i++)
        {
            print(i);
            if(EquipedWeapons[i].gameObject!=null)
            return false;
        }
        return true;
    }```
#

if(EquipedWeapons[i].gameObject!=null)

#

this last line is giving me null ref

#

ah

gentle scroll
#

Yes, because if EquippedWeapons[i] is null, then you can't get the gameObject of EquippedWeapons[i] without a null ref exception.

olive hollow
#

if(EquipedWeapons[i] != null
?

gentle scroll
#

Yep!

#

That should do it.

olive hollow
#

the range of the lists need to be -1 for the count?

#

beacuse I getting a out of ranxe exxeption

gentle scroll
#

Yes:

  public bool isWeaponArrayEmpty(){
    for (int i = 0; i < EquipedWeapons.Length ; i++) // Must be <, not <=
    {
      print(i);
      if (EquipedWeapons[i] != null) return false;
    }
    return true;
  }
#

If you have 5 elements in an array, then the value of array.Length is 5, and the valid indices of that array are 0, 1, 2, 3, & 4.

#

If you're using an array, then you use Length instead of Count.

#

( If you're using a List, then it shouldn't have any null elements. Removing the elements from the List will shrink it. So with a List, if list.Count == 0, then it's empty. )

olive hollow
#

ah ok so it is just < instead of <=

gentle scroll
#

Should be.

#

And for what you want to do, it does seem like an array is the right way to go.

olive hollow
#

yeap I switched back o array again

#

but now it gives me null ref in the second element that I am trying to destroy when I call Destroy(EquipedWeapons["number"].gameObject);

#

if I destroy the element 0 it will give null reff when destroying element 1 vice versa

#

wait

#

it is destroying both

#

with one line

#

give me a sec let me see what is going on

gentle scroll
#

Ok. I recommend using the Visual Studio debugger to try to better understand what's going on. If you don't know how to use the debugger, I highly recommend getting familiar with it. Though it does seem daunting when you first approach it, it can really help you solve problems.

olive hollow
#

yeah , I havent done it yet because the debbuger for unity is not showing up in vscode, even after I installed the addon

gentle scroll
#

Oh, yeah. I love VSCode, but for integration with Unity, the Visual Studio that installs with Unity is better.

olive hollow
#

yeap, the problem is I am using linux and there is no vs for it

gentle scroll
#

Ok, yeah, that makes sense.

#

When I use VSCode on macOS, in addition to installing the debugger plugin, you also need to click the Debug icon on the left and then click "create a launch.json file" to actually make debugging work:

olive hollow
#

yeap I know

#

but after the unity debbuger dont know

#

only .NET

#

and the for loop is stil finding the edstroyied object

#

-.-

#

I am gona try to use the list susgestion

gentle scroll
#

Sorry. I keep trying to get back to my actual work.

olive hollow
#

see ya man

#

thanks for the help

gentle scroll
#

I know that the server says not to PM people, but PM me if you can't get that to work, and I'll try to find some time to help this afternoon.

olive hollow
#

man I fucking ditched all

#

I just made it ```if(EquipedWeapons[0]!=null)
MainWeapon=EquipedWeapons[0];
if(EquipedWeapons[1]!=null)
MainWeapon=EquipedWeapons[1];````

#

it works now

gentle scroll
#

Awesome! 🙂

olive hollow
#

fuck my shit

gentle scroll
#

What if both are null?

olive hollow
#

previusly set it up to null

gentle scroll
#

I mean, is there a situation where MainWeapon is still set to one of them, even if it's null?

olive hollow
#

nah, there need to be at leat one EquipedWeapon to exist a main weapon

gentle scroll
#

Like maybe:

if (EquipedWeapons[0]!=null) {
  MainWeapon=EquipedWeapons[0];
} else if (EquipedWeapons[1]!=null) {
  MainWeapon=EquipedWeapons[1];
} else {
  MainWeapon = null;
}
olive hollow
#

yeap that is about it

gentle scroll
#

Really glad it's working for you now. 🙂

olive hollow
#

                 MainWeapon=null;
                
                if(EquipedWeapons[0]!=null)
                MainWeapon=EquipedWeapons[0];
                if(EquipedWeapons[1]!=null)
                MainWeapon=EquipedWeapons[1];```
#

in this case I have it like this

#

but I can place it in a funcion to make it simpler