#Destroy Transform for Justin-Dude
1 messages · Page 1 of 1 (latest)
So, you tried making it a List<Transform> and manually removing the element from the list, and it's still there?
@olive hollow
Sure thing (though I don't have a lot of time before I need to go)
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;
}```
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.
so I need to destroy the element, push the second now first to the second as well and make a empty first?
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
Instead, since EquippedWeapons is a list, you should use:
EquipedWeapons.Add( Instantiate(_slot.ItemObject.prefab, WeaponPositionSlots[0]).transform );
but I need a certen element to be in slot 0 and the other in slot 1
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){
wait
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.
Destroy(EquippedWeapons[0].gameObject); is givim me null
null ref
affter I call it = null
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
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
Yes, because if EquippedWeapons[i] is null, then you can't get the gameObject of EquippedWeapons[i] without a null ref exception.
if(EquipedWeapons[i] != null
?
the range of the lists need to be -1 for the count?
beacuse I getting a out of ranxe exxeption
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. )
ah ok so it is just < instead of <=
Should be.
And for what you want to do, it does seem like an array is the right way to go.
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
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.
yeah , I havent done it yet because the debbuger for unity is not showing up in vscode, even after I installed the addon
Oh, yeah. I love VSCode, but for integration with Unity, the Visual Studio that installs with Unity is better.
yeap, the problem is I am using linux and there is no vs for it
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:
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
Sorry. I keep trying to get back to my actual work.
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.
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
Awesome! 🙂
fuck my shit
What if both are null?
previusly set it up to null
I mean, is there a situation where MainWeapon is still set to one of them, even if it's null?
nah, there need to be at leat one EquipedWeapon to exist a main weapon
Like maybe:
if (EquipedWeapons[0]!=null) {
MainWeapon=EquipedWeapons[0];
} else if (EquipedWeapons[1]!=null) {
MainWeapon=EquipedWeapons[1];
} else {
MainWeapon = null;
}
yeap that is about it
Really glad it's working for you now. 🙂