- Home /
answerd
Variable not being what it should be
Hi this is a little hard to explain so ill ive uploaded a video https://www.youtube.com/watch?v=gSPrdFPh0pY to explain it better basicly when i instantiate something and i try and access its variable it takes from the latest instead of the one it should be
ive tried loading it from resources but it still does not work as intended. is there a way to make sure the script it separate from the rest of them?
Answer by Broewly · May 30, 2015 at 11:29 AM
try witout the line 45 in your SlotScript.
i already solved this and if i removed line 45 the entire system wouldn't work as thats what it revolves around setting the slot number on the weapon to the slot that its in
Answer by taxvi · May 29, 2015 at 02:02 PM
very interesting question. I wonder what does your SlotScript's line 45 does? to me it seems like it assigns the variable to... itself?
anyways, i had similar issue in my code when I was trying to assign a function with different arguments to my objects' delegates in the foreach() loop. I usually would end up with all the objects having the same delegate.
now my situation was distinct in one thing - the arguments of my delegates' were the objects that foreach was iterating through. I mean - like this:
foreach(Slot slot in slots){
GameObject slotPrefab = (GameObject)Instantiate(slotPrefab);
slotPrefab.GetComponent<Button>().onClick.AddListener( () => SlotFunction(slot) );
}
this is a crude example but you got the point. so the solution was to introduce a temporary variable and feed THAT variable into the SlotFunction instead of what I had, so basically this:
foreach(Slot slot in slots){
Slot tmpSlot = slot;
GameObject slotPrefab = (GameObject)Instantiate(slotPrefab);
slotPrefab.GetComponent<Button>().onClick.AddListener( () => SlotFunction(tmpSlot) );
}
I think this happened because the foreach loop iterates through the collection with a pointer (I'm not an expert in pointers, so I blame everything bad on them). I mean the delegate functions remembered the memory address of the same argument instead of different ones and after the loop was finished they all ended up with the same argument.
I hope this helps bro! I'd love to see where this will go
the line 45 sets the weapons slot number to the slots number that it is in the weapon itself takes away ammo when you shoot so having access to the slot that the weapon is in allows me to show how much ammo i have in the gun ill try your suggestion and see what comes of it
this is weird it apears to be calling all of the slot scripts that have an item on them even tho its not on the script that is actually being used
AH HA!!!!!!! :) i belive i have fixed it atleast for now anyway the only problem now tho is if i have 2 of the same gun it may mess up but ill cross that hurdle when it comes to it
i just had to add an extra condition before sending the slot number
if (weaponhandler.isGunEquiped == true && inventory.Items[slotNumber].itemName == weaponhandler.CurrentlyEquipped)
as all of the slot scripts are being called i will need to work around this by making the script more specific to the item in question
ok I don't understand what that line is doing but cool that you solved it :))
ok i solved it now i was being an idiot and trying to assign the slot number before i had actually euquipped the gun
Follow this Question
Related Questions
How can you send variables to unity3d from the url? 6 Answers
Function variable problem. 1 Answer
Flag structure variable in unity editor 1 Answer
Affect Script Variables using Triggers 2 Answers