Hey, I got this function here and I am trying to access my itemInfo script and get the data I want from it, but there is one issue I don’t know if my way is possible or if it is correct.
pLeftHandWeapon = pStats.pLeftHandWeapon;
pRightHandWeapon = pStats.pRightHandWeapon;
//var
var damage: float;
if (pRightHandWeapon != "none" && pLeftHandWeapon == "none") {
var weapon = gameObject.GetComponent(itemInfo).pRightHandWeapon;
Debug.Log(weapon[1]);
//damage = (*(WeaponDamage*1.75));
}
I want to be able to use a variable to switch the data I need.
For example if I want to get the info for “brick”
if pRightHandWeapon = “brick” I want it to be gameObject.GetComponent(itemInfo).brick
if pRightHandWeapon = “sword” I want it to be gameObject.GetComponent(itemInfo).sword
I want to use the value of the pRightHandWeapon variable at the end so I can get the item data I need to retrieve. I am sorry I don’t know how to explain it very well. It would be easier trying to show what I am trying to accomplish than type it.
I’m not exactly the best guy to answer this since my inventory system and item stats is by far the longest code in my game when you add up all the different parts I need for everything to work.
Here’s a C# example based on what I can see in your code:
public GameObject pLeftHandWeapon;
bool leftHandWeaponEquipped;
float leftHandDamage;
int leftHandItemType;
void Start()
{
//should probably have a PlayerPref to store if weapon equipped
//but for simple example
if (leftHandWeaponEquipped == true)
{
if (leftHandItemType == 1)
{
leftHandDamage = 1.75f;
}
}
else if (leftHandWeaponEquipped == false)
{
leftHandDamage = 0;
leftHandEquippedType = 0;
}
}
//function where you equip/unequip item
void weaponEquipFunction()
{
if (leftHandWeaponEquipped == true)
{
leftHandWeaponEquipped = false;
}
else if (leftHandWeaponEquipped == false)
{
//see what kind of item you are trying to equip
for (int i = 1; i <= possibleItemTypes; i++)
{
if (i == leftHandItemType)
{
//save what kind of item you will have equipped
PlayerPrefs.SetInt("Left Hand Equipped Type", i);
}
}
//call function to determine stats...
itemStatsFunction();
}
void itemStatsFunction()
{
int checkItemTypeInt = PlayerPrefs.GetInt("Left Hand Equipped Type, 0);
if (checkItemTypeInt == 0)
{
leftHandItemEquipped = false;
PlayerPrefs.SetInt ("Left Hand Equipped", 0);
return;
}
else if (checkItemTypeInt != 0)
{
leftHandItemEquipped = true;
PlayerPrefs.SetInt ("Left Hand Equipped", 1);
}
if (checkItemTypeInt == 1)
{
leftHandDamage = 1.75f;
//etc...
}
}
}
So that is just a really generic example of how you might go about doing these kind of things.
I would suggest you have a PlayerPref saved for your game which remembers whether an Item is equipped in the left hand, and what kind of item is equipped in the left hand.
I’m a little confused from your example since it says gameObject.GetComponent which suggests that you are retrieving data from your the gameObject that script is attached to? You should already have a variable ready, you shouldn’t have to getComponent if it’s the same script.
Hard to tell how you are setting it up exactly. Hopefully this example will help a little I never use JavaScript either so sorry for C# example.