- Home /
changing variable based on name of objects in array.
I'm programming a really simple top down shooter to get used to Unity/programming. I use the mouse wheel to select the current weapon. The list of weapons available is an array, and new weapons are added when the player walks over collectibles. That all work great.
On the GUI I'd like to display the weapons in the array and the ammo for each one. The 3 x weapons are: sword, pistol and shotgun.
Here is the code I'm using for the OnGUI:
function OnGUI()
{
ps = GetComponent(playerScript);
var yOffset : float = 35;
for (var i : int = 0; i <= weapons.Count-1; i++)
{
GUI.Label (Rect (10, 10 + yOffset * i, 100, 20), weapons[i].name + ":" + ps.pistolAmmo); // <---problem is here.
}
}
The problem is that Whilst I can specify the ammo variable from another script, I can't work out how to do so dynamically? If the name of the object at index [1] is shotgun for example, how do I specify that the shotgunAmmo variable should be displayed?
Is there some way to achieve this, so I can call (in terrible pseudo-code):
ps.(weapons[I].name)Ammo
or another simple solution I'm overlooking?
Thanks
What is weapons - a class? Define maxAmmo, curAmmo, usesAmmo (bool) in your class and then it'll be weapons[i].curAmmo, weapons[i].maxAmmo, weapons[i].usesAmmo. Would that work?
Answer by Seth-Bergman · Sep 09, 2013 at 10:08 PM
Assuming that weapons is a GameObject Array:
the suggestion alluded to by @getyour411 is a viable alternative..
create a separate script and call it "Weapon". Give this new script the necessary data members for any given weapon:
var myName : String;
var ammoCount : int;
then attach a copy of the script to each weapon. Use an array of "Weapon" instead, and you have much more control over what is at your fingertips.
(to store the Weapon reference, you would simply use gameObject.GetComponent(Weapon) for each weapon)
EDIT:
To elaborate.. you can access the script from your current GameObject array by replacing:
weapons[i].myName
with
weapons[i].GetComponent(Weapon).myName;
however, it is better to use GetComponent only in the start function to initialize the variable.. Using it constantly in OnGUI will cost big time on performance. You are probably better off using an array of Weapon instead, to solve this issue more simply, in my opinion anyway...
if weapons is an array of Weapon rather than GameObject, you can Add it to the list using:
invList.weapons.Add(invList.pistol.gameObject.GetComponent(Weapon));
(based on the example you gave)
since you only call this for one frame it's not SO bad...
Ok, I get the concept of what you're both saying, but am struggling with the implementation.
I now have 3 x game objects: sword, pistol and shotgun. On each object I have attached a script called weapon:
var myName : String;
var ammoCount : int = 0;
var shotVel : float = 15.0;
var shotVar : float = 0.0;
In my weapon switching script I try and use GetComponent as Seth described:
function OnGUI()
{
sword.GetComponent(weapon);
var yOffset : float = 35;
for (var i : int = 0; i <= weapons.Count-1; i++)
{
GUI.Label (Rect (10, 10 + yOffset * i, 100, 20), weapons[i].myName + ":" + weapons[i].ammoCount);
}
GUI.Label (Rect (10, 140 , 100, 20), currentWeapon.name);
}
But this throws the following errors: "myName is not a member of UnityEngine.GameObject." & "ammoCount is not a member of UnityEngine.GameObject."
However, both of those clearly are members of the game object, as I have described them as such and can see them in the inspector etc.
It may be that I've misunderstood you when you say to use an array of "weapon" ins$$anonymous$$d. I did try this, but it played havoc with my other scripts, such as adding to the array:
invList.weapons.Add(invList.pistol.gameObject);
Once again I'm stumped. Grrr.
and trying:
weapons[i].weapon.myName
doesn't work either :(
Come on guys, I hate to bump the thread, but I've tried everything I can think of and read loads online and I'm just not getting it.
Can anyone shed any light? Why can't I access the variables on the weapon script in each object?
sorry I've been really busy.. I will elaborate my answer based on your code
Hi Seth.
That's a really clear explanation and I can see where I was going wrong. I'm nearly there with the array of "weapon" but have hit a small snag.
Previously I dragged the array objects from the library into the inspector, (sword, shotgun etc),which worked fine. However if I make my array of type : weapon, ins$$anonymous$$d of GameOject I can't assign the script in the inspector. Any ideas how to do this?
I know it's a noobish question, so sorry to take up more of your time.
Your answer
Follow this Question
Related Questions
How to iterate through array? 1 Answer
Getting information from an array 1 Answer
IndexOutOfRangeException: Array index is out of range. UnityEditor.AnimationSelection.GetClipAtIndex 1 Answer
Index Out of Range Exception - can't figure it out 1 Answer
Adding and Removing to a Inbuild Array 2 Answers