List not updating as expected

Hi there,

NOTE I posted this earlier today, and it has vanished without trace as far as I can see. If this was deleted for some reason please let me know why before deleting again!

I am trying to recurse through a bunch of GameObjects in a list:

// get the relative position of the weapons to the ship
List<Transform> weaponPositions = ship.GetWeaponLocations(); // works

// set the positions of the weapons
for(int i = 0 ; i < equippedWeapons.Length && i < weaponPositions.Count ; i++)       
{
    equippedWeapons_.GetComponent<Weapon>().setPosition(weaponPositions*);*_

}
However it seems to apply the last ‘weaponPositions’ to everything. So weaponPositions is a list of type Transform, and I want to set the position of weapon[0] to the position of weaponPosition[0] for example.
The problem is that when it gets to the third weapon (the last in this case), it applies the weaponPosition[2] position to each of the weapons that has come before, not just the i’th index.
The weapons are a SerailizedField:
[SerializeField] GameObject[] equippedWeapons;
In weapon the setWeapon method is simply:
Transform positionOfWeaponOnShip;
public void setPosition(Transform pos)
{
positionOfWeaponOnShip = pos;
}
The question I am asking is, am I using the List in the correct way here? Im stumped as to why it isnt only applying this to the i’th value. Im also not sure what else I can put here which would be useful…
Thanks,
Bogo

Doh! -1 not needed sorry. Lol.

Ok I think I may get your problem.

By dragging the prefab to the List, it’s just a reference to the object. What you need to do is instantiate the weapons at each point.

Create a List of all possible Weapons(GameObjects), a List of Equipped(int) (the int is a reference to the prefab in Weapons) and a List of Positions(Transform).

You then need to loop through Equipped and Positions instantiating a weapon at each position:

weapon = Instantiate(Weapons[Equipped<em>],Position<em>.position,Position_.rotation,Position*);*_</em></em>

This should instantiate an instance of the weapon at the position, using the position as it’s rotation and parent. Each time you change equipped weapons, you’ll want to destroy them all and then run this again.

Hi KevRev,

Thanks - but I need to compare both because the amount of weapons that can be equipped is either limiting, or the amount of weapons currently equipped. It also doesn’t need to be -1 as its less than only, not less than or equals to.

What has become apparent using GetInstanceID() is that the prefab is I am putting onto the equippedWeapons list is the same. So the same weapon instance is being applied multiple times. If I duplicate the prefab twice and put those three individual prefabs onto the equippedWeapons List this works perfectly.

Now the issue is - I dont want three prefabs of the same weapon, I want to put three of the same prefab into the inspector and have that as three separate instances. This is the problem I am now working on :slight_smile:

The List is instantiated as just:

[SerializeField] List<GameObject> equippedWeapons;

The weapons are added in the inspector, dragged and dropped from Weapon prefabs. This is now what I think is the problem. The weapon locations are children of a Ship prefab, so just locations which are placed on a Ship prefab. This doesnt appear to be a problem - just adding them to the appropriate weapons is the problem.

I hope this makes sense,

Thanks so much for your help!

Bogo

Doh! -1 not needed sorry. Lol.

Ok I think I may get your problem.

By dragging the prefab to the List, it’s just a reference to the object. What you need to do is instantiate the weapons at each point.

Create a List of all possible Weapons(GameObjects), a List of Equipped(int) (the int is a reference to the prefab in Weapons) and a List of Positions(Transform).

You then need to loop through Equipped and Positions instantiating a weapon at each position:

weapon = Instantiate(Weapons[Equipped<em>],Position<em>.position,Position_.rotation,Position*);*_</em></em>

This should instantiate an instance of the weapon at the position, using the position as it’s rotation and parent. Each time you change equipped weapons, you’ll want to destroy them all and then run this again.