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 OmegaNemesis28 · Feb 16, 2013 at 12:21 PM · c#gameobjectinventorypassing

Passing gameObjects to Another Script: NullReference Exception

I'm having difficulty passing gameObjects from one script to another.

I have a function called addItems(1-object-here) to add to an arraylist in Inventory.cs

I have a function called pickUpItem() to called on addItems in PickupItem.cs

It is giving me an error that there is a nullreference exception I'm trying to follow the Unity documents for this, and I have it nearly exactly the same.

In PickUpItems.cs:

 // Update is called once per frame
     void Update () {
     PlayerNearby();
     
     if(Input.GetKeyDown("space"))
     {
      pickupItem();
      }
     }
     
     void pickupItem()
     {
             if (nearby)
     {
         Debug.Log (nearby);
         GameObject.Find("player").GetComponent().addItem(this.gameObject);
         Destroy (this);
     }
     }

In Inventory.cs:

 public class Inventory : MonoBehaviour {
      public ArrayList inventoryL = new ArrayList();
 
 
     // Use this for initialization
     void Start () {
         
     }
     
     public void addItem(GameObject item)
     {
         inventoryL.Add(item);
     }
     
     public void removeItem(GameObject item)
     {
         for(int i = 0; i < inventoryL.Count; i++)
         {
             if(inventoryL[i] == item)
             {
                 inventoryL.RemoveAt(i);
             }
         }
     }
     
     // Update is called once per frame
     void Update () {
     
     }
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
0
Best Answer

Answer by Bluestrike · Feb 16, 2013 at 01:35 PM

Unless c++ works different as javascript your not telling what component to acces:

 GameObject.Find("player").GetComponent().addItem(this.gameObject);

I think you want that to be

 GameObject.Find("player").GetComponent(Inventory).addItem(this.gameObject);

Also you are destroying the pickup so I am not sure what kind of effect that gives on the List, perhaps a null refence as the item where the list entry links to is destroyed. What makes me ask why you need to store something in a list if you destroy it right after.

The way pikups work in my game is by telling my player what item is unlocked and add that to a list or array.

In case you want to prepare for possible multiplay you can consider hiding the pickup instead of destroying and set a reset timer to make it pop again.

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

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

10 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

Related Questions

C# Adding Components From Other Gameobjects 3 Answers

Camera to follow a target within a circle? 1 Answer

Assigning current color to a variable for fade out (C#) 0 Answers

Destroy + Score 1 Answer

How to modify specific GameObjects in an array of GameObjects? 1 Answer


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