S
S
s2d1ent2021-06-01 21:51:02
C++ / C#
s2d1ent, 2021-06-01 21:51:02

How to create object in Unity object?

Hello. There was such a problem that it is impossible to shove noteTile as a child object in the Journal on which the script is hung.

public class Journal : MonoBehaviour
{
    List<Note> noteList = new List<Note>();
    [SerializeField] GameObject noteTile;
    string noteName, noteDescription;

    bool nul = true;

    void Start ()
    {
        CheckNotes();
    }

    void CheckNotes ()
    {
        if (nul)
        {
            noteName = "note name";
            noteDescription = "note description";
            noteList.Add(new Note(noteName,noteDescription));
            noteTile.name = noteName;
            Instantiate(noteTile,gameObject);
        }
    }

}
class Note
{
    public string name;
    public string description;
    
    public Note(string name, string description)
    {
        this.name = name;
        this.description = description;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Boronnikov, 2021-06-03
@s2d1ent

If noteTile- this is a reference to an object that is already in the scene, then you do not need to instantiate the object, but simply change the parent
noteTile.transform.parent = parent.transform;
where parent- this GameObjectis where you want to place it noteTile
. If it is noteTile not instantiated, then you should first instantiate, and only then set various parameters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question