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[i].GetComponent<Weapon>().setPosition(weaponPositions[i]);
}
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
Can't see if you are using the List correctly with seeing how you populate it.
Can you show your code that initialises the List, and the GetWeaponLocations that I'm guessing populates it.
It could also be the two values you are using in the following loop:.
for(int i = 0 ; i < equippedWeapons.Length && i < weaponPositions.Count ; i++)
You only need to loop through one variable, adding a second in the OR statement is a bit confusing.
Try
for(int i = 0 ; i < equippedWeapons.Length - 1 ; i++)
Also notice the -1. Your first position is index 0. So if you had 5 items, the last index position will be 4 not 5. By running the loop to 5, you potentially introduce an unexpected item into the list.
I hope this helps?
Answer by KevRev · Apr 22, 2019 at 07:32 AM
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[i]],Position[i].position,Position[i].rotation,Position[i]);
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.
Converted to comment for now. Hopefully OP will change it to Best Answer.
Hi $$anonymous$$evRev, sorry, been busy with actual work for the past few days so will look at this again when I have a few $$anonymous$$utes. Thanks for the help, and ill put it as best answer when ive tried it all out :)
Hi $$anonymous$$evRev,
This worked perfectly, thanks! How do I make it the top answer?
Bogo
Excellent, glad it worked :)
You should have a button you can click now I've converted this to an answer.
Answer by Bogomip · Apr 22, 2019 at 06:44 AM
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 :)
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
Your answer
Follow this Question
Related Questions
How can I get the player to face the direction it is going in a Unity 2D Game? 0 Answers
Unity 2D Build Error 0 Answers
LookAt in 2d is not working Again :/ 0 Answers
How to change animator states when a key is held through a C# script for a sprite? 0 Answers
Activating multiple images under one canvas via multiple objects 0 Answers