- Home /
Binding Key to first item in array
Hi guys- So first off I want to apologize if this is an obvious question, but I'm very new to coding and I'm working with some JS code that I bought from the Asset store that I'm having a hard time configuring. I've searched for a full week for answers how to do this, but I haven't had any luck in figuring it out.
Basically what I'm trying to do is to expand on an Inventory System to make an action bar for spells. I'd like spells to be "looted" from monsters around a zone, and by default they go to a player's inventory. The player then equips them in the action bar where they can be initiated by pressing keys on the keyboard.
Here are the relevant parts of the code I'm working with (at least I hope so, I didn't want to post the entire code base without permission). I have the following code in InventoryManager.js attached to a Game Object of the same name:
class InventoryItem {
public var itemName : String = "New Item";
public var itemIcon : Texture = null;
public var slotType : SlotType = SlotType.Bag;
public var itemType : ItemType = ItemType.Consumable;
public var containerSize : int = 0;
public var itemObject : Rigidbody;
public var spellEffect : GameObject;
public var isUnique : boolean;
public var isIndestructible : boolean = false;
public var isQuestItem : boolean;
public var weaponDamageMin : float = 0;
public var weaponDamageMax : float = 0;
public var weaponStrength : float = 0;
public var healthBonus : float = 0;
public var strengthBonus : float = 0;
public var armorBonus : float = 0;
public var isStackable : boolean = false;
public var stackSize : int = 0;
public var destroyOnUse : boolean = false;
}
function SummarizeSpells () {
for (var q : int = 0; q < memorized.Count; q ++) {
if (memorized[q] != null) {
Spells = memorized[q].spellEffect;
}
}
So what I'm trying to do is drop an empty GameObject with some code for a specific spell attached to it into the spellEffect var on an item.
I have the following code attached to my player:
function Update ()
{
var otherGameObject = GameObject.Find("InventoryManager");
if (otherGameObject == null)
Debug.Log("There is no game object named InventoryManager!");
var inventoryManager = otherGameObject.GetComponent(InventoryManager);
if(inventoryManager == null)
Debug.Log("There is no InventoryManager component attached to this game object!");
inventoryManager.Spells;
}
EDIT: Changed around the update function in the player script thanks to jbara's help which is much better than I had before (but I still get an error "Expressions in statements must only be executed for their side effects"). But I think I was a little unclear about where my confusion was. I'm pretty sure I understand how to access a script with a specified name attached to a game object. My question is how you would access a script attached to a game object that's defined in the inspector rather than in the script itself.
What I'm trying to do (unless there's a better way) is to drag and drop a game object with a script on it into the public var "spellEffects." When I press 1 on my character script, I'd like to execute whatever is in the Update function of the script attached to the game object. Additionally, I'm trying to get "1" to point to the spellEffect slot of the first item in the array in function SummarizeSpells.
Again, really sorry for the mixup and sorry if I'm horrible at explaining things :)
Usually, when you get a "Null Exception" of some sort, it means that you are trying to use an object that has no value (null value) in a way that it can't be used when it is a no value. Usually, "using it in a way that it can't be used when it has no value" means you are trying to access the data or call the functions, which just don't exist when there's nothing there.
So, either GameObject.Find() is not finding a game object named "Inventory$$anonymous$$anager", or GetComponent() is not finding a script of the type Inventory$$anonymous$$anager
. Either way, one of them is returning a null value, and because of that you can't reference any members of that class instance.
If otherGameObject is null, then its breaking because of the call to GetComponent(). If the value returned by GetComponent() is null, then its breaking because you're trying to access Spells.
I suggest that you do something like this:
if(Input.GetButtonDown("Fire1"))
{
var otherGameObject = GameObject.Find("Inventory$$anonymous$$anager");
if (otherGameObject == null)
Debug.Log("There is no game object named Inventory$$anonymous$$anager!");
var inventory$$anonymous$$anager = otherGameObject.GetComponent(Inventory$$anonymous$$anager);
if(inventory$$anonymous$$anager == null)
Debug.Log("There is no Inventory$$anonymous$$anager component attached to this game object!");
inventory$$anonymous$$anager.Spells;
}
This will let you figure out which one is null, and that way you can take the appropriate steps to fix it. perhaps you don't have the right script attached, or maybe you didn't give a game object the proper name.
Ohh ok. That's definitely a better way of writing the statement.
Really sorry about this, but I think I need to edit the original question because it's giving me a different error than I thought it was. The error is "Expressions in statements must only be executed for their side effects." I'll search around a bit more to see if I can figure out why that is, but I suspect it's because it's referencing a Game Object and not a function on the script attached to the game object. So when I press 1 it doesn't know what to do.
It's really not allowed to post any code at all without permission from the author. To be on the safe side I've edited your post to remove the code. You can post anything that you've written, but that's all.
Eric, a good chunk of that was my own code. The ability to make spells obviously wasn't included in the code I bought or I wouldn't be here. Can I add those parts back? And not to be argumentative, but could you ask me next time and let me remove it myself? It took me a while to remove the parts of the code that I didn't write that pertained to the question.
Revisions are always kept when using the edit feature. That's why I removed it all, since it's easy enough to put it back; just make sure it's only your own code.
Your answer
Follow this Question
Related Questions
Storing Inventory Item Properties 2 Answers
How do you debug arrays of objects? 1 Answer
Setting generic list length 2 Answers
Array of random loot items inside GUI.window 1 Answer
Mysterious crash involving an array 2 Answers