Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by generic cereal · Apr 23, 2013 at 05:50 PM · javascriptarrayinventory

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 :)

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hoy_smallfry · Apr 23, 2013 at 06:09 PM 0
Share

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.

avatar image generic cereal · Apr 23, 2013 at 06:29 PM 0
Share

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.

avatar image Eric5h5 · Apr 23, 2013 at 07:31 PM 0
Share

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.

avatar image generic cereal · Apr 23, 2013 at 07:38 PM 0
Share

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.

avatar image Eric5h5 · Apr 23, 2013 at 07:41 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges