Search Unity

Question Accessing array from another script

Discussion in 'Scripting' started by liquidsht, Sep 17, 2020.

  1. liquidsht

    liquidsht

    Joined:
    Jun 4, 2020
    Posts:
    49
    I am trying to build a save system with which needs to save inventory the player is carrying. I am using an asset called Corgi Engine for the controller which also has inventory system built in.

    The inventory in the corgi engine is an array so when I save, I am trying to access the array with my save point script, the Corgi Engine script looks like this now(I am only showing the codes I think is relevant as I am sure the Corgi Engine developer wont want me to show all his codes):
    Code (CSharp):
    1. namespace MoreMountains.InventoryEngine
    2. public class Inventory : MonoBehaviour
    3. {public InventoryItem[ ] Content;
    My save manager Script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using MoreMountains.InventoryEngine;
    5. public class SavePoint : MonoBehaviour
    6. {
    7.     GameObject player;
    8.     GameObject gameManager;
    9.     EasySave easySave;
    10.     HealthWithCameraShakeExtension health;
    11.     GameObject savePoint;
    12.     Inventory inventory;
    13.  
    14.  
    15.     void Start()
    16.     {
    17.         player = GameObject.FindWithTag("Player");
    18.         easySave = GameObject.Find("EasySaveManager").GetComponent<EasySave>();
    19.         inventory = GameObject.Find("Inventories").GetComponent<Inventory>();//grabs inventory from MoreMountain inv engine
    20.      
    21.      
    22.     }
    I am able to see Inventory class from asset with intellisense, but I dont know how to go about accessing the InventoryItem[ ] array from my savepoint script. TIA.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,908
    Code (CSharp):
    1. InventoryItem[] inventoryArray = inventory.Content;
     
  3. liquidsht

    liquidsht

    Joined:
    Jun 4, 2020
    Posts:
    49
    Thanks PraetorBlue, I put the line in Start(), there is no errors in visual studio but I am getting NullReferenceException error in the editor. Do you know what might cause this?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,908
    Your GameObject.Find couldn't find anything, or the object it found didn't have an Inventory component on it.