How to Instantiate an Object in C#

Asked

Viewed 1,639 times

2

How do I instantiate an Object in another class?

I’m trying this way:

        GameObject Cozinha = new GameObject("CenaCozinha");
        CenaCozinha coz = Cozinha.AddComponent<CenaCozinha>();

But I believe that so he is creating a new Object, hence all that I have already put in the original 'Kitchen' (how to feed lists for example), will not exist in the new.

  • 1

    You must pass the instance of the object created by a function and save to a property in its new class.

  • um, can exemplify pfvr?

  • I’ll post an answer!

  • Okay, thank you :D

2 answers

2


You must pass the instance of the object created by a function and save to a property in its new class. Example:

First create a class that has a property with the desired class type:

public class NewClass
{
    private GameObject _object;
    public void SetObject(GameObject obj)
    {
        _object = obj;
    }
}

After that, enter the new class and pass the object through the created void

GameObject Cozinha = new GameObject("CenaCozinha");
CenaCozinha coz = Cozinha.AddComponent<CenaCozinha>();

NewClass newClass = new NewClass();
newClass.SetObject(coz);
  • Calm down, let me understand, my little Class will not be changed, all this code will be in Newclass (which in my case is Food)...?

  • 1

    You didn’t make it clear which class is the main one. Newclass would be the class you want to reuse the instance of Cenacozinha. You must instantiate at least once.

  • I’ll put my classes in response

  • Okay, this one up

  • 1

    From what I understand, you have the Little Girl Class that instantiates the Food Class, right? Then you should put a property in Food with type Cenacozinha and assign its value at the time it is instantiating, Example: public CenaCozinha Cena; novoAlimento.Cena = this;

  • Oh man! It worked! Thank you! D

Show 1 more comment

0

Here:

public class CenaCozinha : MonoBehaviour { 
//Os Alimentos
public GameObject Alimento;
public GameObject Agua;

public List<Alimentos> alimentosCozinha = new List<Alimentos>();    

void Start () {
}

void Update () {
    foreach (Alimentos al in alimentosCozinha) {
        if (al.nomeAlimento == "agua" && al.quantidade > 0 && !Agua.activeSelf) {
            al.meshAlimento.SetActive(true);
        }
    }
}

public void ComprarAgua() {
    foreach (Alimentos al in alimentosCozinha) {
        if (al.nomeAlimento == "agua"){
            al.quantidade++;
            return;
        }
    }
    Alimentos novoAlimento = Alimento.AddComponent<Alimentos>();
    novoAlimento.id = "agua";
    novoAlimento.nomeAlimento = "agua";
    novoAlimento.quantidade = 1;
    novoAlimento.meshAlimento = Agua;
    alimentosCozinha.Add(novoAlimento); 
}}

And

public class Alimentos : MonoBehaviour { public string id;
public int quantidade;
public string nomeAlimento;
public GameObject meshAlimento;
public string id;

public void Start () {
}

public void Update () {
}

public void OnCollisionEnter2D(Collision2D colisao) {
    if (!colisao.gameObject.CompareTag("Agua")) {
        GameObject Cozinha = new GameObject("CenaCozinha");
        CenaCozinha coz = Cozinha.AddComponent<CenaCozinha>();
        Debug.Log(coz.Alimento);
        Debug.Log(coz.Agua);
        foreach (Alimentos al in coz.alimentosCozinha) {
            Debug.Log(Cozinha);
        }
    }
}}

In Debug.Log(coz.Food); Debug.Log(coz.Water); It returns me Null.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.