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 ccrh · Aug 18, 2014 at 07:41 PM · inventoryinventory system

AddComponent Question

Here is the situation, I have a script that I use to define my items:

 public GameObject bigDBlock; //short for bigdefaultblock or cube
 public static Item bigDCube;
 
 void Start () {
     defineItems();
 }
 
 public class Item { 
                     
     public string name = "Empty";
     public GameObject itemObject;        
     public float salt;   //short for spawn altitude
     public void OnLeftClick(Vector3 mypoint) {
         itemObject.AddComponent<ItemID> ().ThisItem = this;
         print ("Item name:" + itemObject.GetComponent<ItemID>().ThisItem.name);//Here
         mypoint.y += salt;
         Instantiate(itemObject, mypoint, Quaternion.identity);
     }                
 }
 
 void defineItems() {
         bigDCube = new Item ();
         bigDCube.itemObject = bigDBlock;
         bigDCube.salt = 2f;
         bigDCube.name = "BigDCube";
 }

But when I call OnLeftClick, it throws the error NullReferenceException on the line that has //Here at the end. It seems to me that the statement above that line is the problem but it compiles fine. Is there something wrong with the way I call AddComponent?

Can anyone maybe give a reason why, as I have been fiddling with it for the longest time and I feel like things like this are really simple and I am looking at the wrong thing.

Additionally, The way I use this is I have an array of items that I use as the player's inventory and when the player right clicks it calls playerInventory[currentItemSlot].OnLeftClick(pointIamlookingat); to do whatever the left click action is for that item. There are currently 4 items, but they are very similar in format so I included just one for ease of reading. when the game calls Start and then defineItems, it defines the items in a way the rest of the game can use, by copying the pre-existing default items

Oh yes, here is the ItemID class as well

 using UnityEngine;
 using System.Collections;
 
 public class ItemID : MonoBehaviour {
 
     public ItemMan.Item ThisItem;
 
 }

The way this is supposed to work is the player has their array of Items in their inventory and when they call the leftclick action for the currently held item (which would be the block above) it is supposed to copy the gameobject from inside the Item into the actual game, attach that ItemID script to it then set the inventory slot to an empty object. the opposite is supposed to occur when the player picks up an item: the gameobject.ItemID.ThisItem is copied into the player's inventory (space permitting) then the gameobject is destroyed

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by rutter · Aug 18, 2014 at 07:55 PM

It's important to remember that GetComponent returns the first matching component. If you have multiple such components attached, there's no guarantee that GetComponent will ever return the very same object that you just attached with AddComponent.

Instead, you should probably use the return value from AddComponent:

 MyComponentType myComponent = someGameObject.AddComponent<MyComponentType>();

If you intend to use it repeatedly, that component could be stored in a lookup table, perhaps a List or Dictionary, or some other collection. That way, you can be more specific about which object references which particular component, and you can avoid unnecessary GetComponent calls.

Comment
Add comment · Show 3 · Share
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 ccrh · Aug 18, 2014 at 08:01 PM 0
Share

hehe, yes well, I was looking around and found something that said something about Getcomponents[] returning all components of that type in the order they are in in the editor... In investigating that, I found that part of this was essentially attached HUNDREDS of the ItemID script to the 4 prefabs I currently have... I feel like that might have been interfering with the whole process of "getting the right value"

avatar image ccrh · Aug 18, 2014 at 08:10 PM 0
Share

Also, as it is set up, the GetComponent call should only have to be made once (assu$$anonymous$$g the player has inventory space), it isnt made every frame or update.

avatar image ccrh · Aug 18, 2014 at 08:22 PM 0
Share

Bwahahaha, It works! A couple more modifications and the general inventory system will be CO$$anonymous$$PLETE! Thanks!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Defining an Item type in C# 1 Answer

Having scripts interact 1 Answer

Instantiate a destroyed GameObject by its original Prefab from the "Assests" folder? 0 Answers

how to detect wich item i have in my inevntory 0 Answers

Best way to implement RPG Inventory System (w/ Sized Items) 0 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