Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Trying to use MonoBehaviour new, not working

Discussion in 'Scripting' started by Sajid, Nov 16, 2015.

  1. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    Alright so I have a simple "store" UI that pops up when you hit a key. When you buy something by clicking one of the UI buttons, I want it to add that item to my list of items in my inventory script.

    Here is the inventory script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Inventory : MonoBehaviour {
    5.     [SerializeField]
    6.     private invItem[] invItems = new invItem[10];
    7.  
    8.     public void addInvItem(int index, invItem inItem)
    9.     {
    10.         for(int i = 0; i <= invItems.Length; i++)
    11.         {
    12.             if (i == index)
    13.             {
    14.                 invItems[index] = inItem;
    15.             }
    16.         }
    17.     }
    18. }
    19.  
    Here is the invItems that it is referencing:
    Code (CSharp):
    1. public class invItem : MonoBehaviour {
    2.     public string itemName;
    3.     public int cost;
    4.  
    5.     public invItem(string inName, int inCost){
    6.         itemName = inName;
    7.         cost = inCost;
    8.     }
    9. }
    and here is the button script:
    Code (CSharp):
    1.     [SerializeField]
    2.     private GameObject GameControl;
    3.     void Start()
    4.     {
    5.  
    6.     }
    7.     public void addBasicTurret()
    8.     {
    9.         invItem basicTurret = new invItem("Basic Turret", 5);
    10.         GameControl.GetComponent<Inventory>().addInvItem(0, basicTurret);
    11.     }
    I don't get any errors, but when I try and use the button in order to add an invItem object into my array, I get this warning:
    Any idea what I'm doing wrong here? I kinda understand that the warning is trying to tell me not to use "new" but I'm not sure how else to do it.
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    It gives you alternatives right in the error message:
    Code (csharp):
    1. public class invItem {
    If you aren't ever attaching it as a component to a GameObject.
     
  3. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    Problem is, when I do this, my array doesn't show up in the inspector, which is causing me a lot of hassle. I tried this originally but it wouldn't work because of that.

    Is there any way I can take out monobehaviour and keep it in the inspector?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Add [System.Serializable] just before the declaration of the class.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Alternately, you could leave it as a MonoBehaviour and attach it as a script, but then use gameObject.AddComponent as the warning suggested to.
     
  6. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    Wow, that worked perfectly! Thanks a bunch.