2

I try to make an vertical inventory, like i have show in picture. Inventory

I want to be able to add slots in number of items available, and and the create a schroolbar to view all slots available. i will use switch to switch the sprites. I need to create an instance of the prefab of the slot for each item picked. I have try my self implement ip but it give me an 2 errors wen i try to instatiate de prefab. this is the errors:

1 Setting the parent of a transform which resides in a prefab is 
disabled to prevent data corruption.
UnityEngine.Transform:SetParent(Transform, Boolean)
additem:CreateA() (at Assets/inventory ultimate pt/additem.cs:49)
additem:Start() (at Assets/inventory ultimate pt/additem.cs:35)

the line in question is
the void createA prefabtoinstatiate.transform.SetParent(panel.transform, false);


2 NullReferenceException: Object reference not set to an instance of an 
object
additem.CreateA () (at Assets/inventory ultimate pt/additem.cs:50)
additem.Start () (at Assets/inventory ultimate pt/additem.cs:35)

the line in question is
title_text.text = title_text1.text;

this is my code so far.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class additem : MonoBehaviour {
public static int havepick = 0;
public Transform prefabtoinstatiate;
public GameObject panel;
public static Text title_text;
public static Text description_text;
public static Text amount_text;
public static Sprite image;
public static int amountx = 0;

public Text title_text1;
public Text description_text1;
public Text amount_text1;
public Sprite image_icon_small_healling;
public Sprite image_icon_medium_healling;
public Sprite image_icon_big_healling;
public Sprite image_icon_small_stamina;
public Sprite image_icon_medium_stamina;
public Sprite image_icon_big_stamina;
public Sprite image_icon_small_magic;
public Sprite image_icon_medium_magic;
public Sprite image_icon_big_magic;
public Sprite image_icon_small_defence;
public Sprite image_icon_medium_defence;
public Sprite image_icon_big_defence;

public int image_number_count;
// Use this for initialization
void Start () {

    CreateA ();

}

// Update is called once per frame
void Update () {
    description_text1.text = "Small healling Potion " +
        "Give You 10 Points Of Healling";
    amount_text1.text = "Amount: " + amountx;
    title_text1.text= "Small Healling Potion";

}
public void CreateA() {

    prefabtoinstatiate.transform.SetParent(panel.transform, false);
    title_text.text = title_text1.text;
    description_text.text= description_text1.text;
    amount_text.text =amount_text1.text;
}
public void changeimages()
{
    switch (image_number_count) 
    {
        case 0:
        GetComponent <Image>().sprite = image_icon_small_healling;
        image_number_count++;
        break;
        case 1:
        GetComponent <Image>().sprite = image_icon_medium_healling;
        image_number_count++;
        break;
        case 2:
        GetComponent <Image>().sprite = image_icon_big_healling;
        image_number_count++;
        break;
        case 3:
        GetComponent <Image>().sprite = image_icon_small_stamina;
        image_number_count++;
        break;
        case 4:
        GetComponent <Image>().sprite = image_icon_medium_stamina;
        image_number_count++;
        break;
        case 5:
        GetComponent <Image>().sprite = image_icon_big_stamina;
        image_number_count++;
        break;
        case 6:
        GetComponent <Image>().sprite = image_icon_small_magic;
        image_number_count++;
        break;
        case 7:
        GetComponent <Image>().sprite = image_icon_medium_magic;
        image_number_count++;
        break;
        case 8:
        GetComponent <Image>().sprite = image_icon_big_magic;
        image_number_count++;
        break;
        case 9:
        GetComponent <Image>().sprite = image_icon_small_defence;
        image_number_count++;
        break;
        case 10:
        GetComponent <Image>().sprite = image_icon_medium_defence;
        image_number_count++;
        break;
        case 11:
        GetComponent <Image>().sprite = image_icon_big_defence;
        image_number_count++;
        break;
    }
}
}
  • 2
    Read up on Row Layout & Scroll rect. Then just instantiate your prefabs as children and they will end up perfectly and scrollable. Regarding your exceptions; that's not how you instantiate something. You must use `Instantiate(original: prefabtoinstatiate, parent: panel.transform)` – Fredrik Schön Aug 01 '19 at 20:50
  • 1
    @FredrikSchön I have try your code and it give this error Assets/inventory ultimate pt/additem.cs(48,17): error CS1501: No overload for method Instantiate' takes 2' arguments – helder ventura Aug 01 '19 at 21:11
  • @helderventura emm yes it does: [`Instantiate(Object original, Transform parent);`](https://docs.unity3d.com/ScriptReference/Object.Instantiate.html) did you maybe somewhere define your own method called `Instantiate`? And I guess a combination of [`ScrollRect`](https://unity3d.com/de/learn/tutorials/modules/beginner/ui/ui-scroll-rect) and [`VerticalLayoutGroup`](https://docs.unity3d.com/Manual/script-VerticalLayoutGroup.html) is exactly what you are looking for – derHugo Aug 02 '19 at 06:52
  • your first error is actually pretty self explanatory: `Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.` you are trying to set the parent to `panel` which seems to be a prefab not an object in the scene. The second error: Either `title_text` or `title_text1` is not set so add breakpoints and [Debug](https://unity3d.com/de/learn/tutorials/topics/scripting/debugging-unity-games-visual-studio) your code line by line to see what happens and why – derHugo Aug 02 '19 at 06:56
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – derHugo Aug 02 '19 at 06:56

0 Answers0