S
S
Sergey Panov2018-05-30 11:28:50
C++ / C#
Sergey Panov, 2018-05-30 11:28:50

How to work with created objects in code?

Good day, friends.
There is a code

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

public class Gameplay : MonoBehaviour {

    string Word;
    public GameObject PrefabCharCell;
    public GameObject tempCell;
    public Transform GridCellPos;
    
  void Start ()
    {
        Word = "Meet"; 
        foreach (char ch in Word.ToUpper().ToCharArray())
        {
            tempCell = Instantiate(PrefabCharCell); 
            tempCell.transform.SetParent(GridCellPos,false);   
        }  
    }
}

As a result, 4 objects of type GameObject are created in tempCell. And how can I access them and work with them in the future, in other methods and functions?
For example, for the first tempCell element, I want to change the Text property, already in another method, and for the second element, I want to change the Text property to a different value. How to do it ?
Thank you very much in advance .

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
VoidVolker, 2018-05-30
@select8

Pass selected objects to other methods and functions.

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

public class Gameplay : MonoBehaviour {

    string Word;
    public GameObject PrefabCharCell;
    public GameObject tempCell;
    public Transform GridCellPos;
    
    void SomeMethod(GameObject tempCell){ 

    }

    void Start ()
    {
        Word = "Meet"; 
        foreach (char ch in Word.ToUpper().ToCharArray())
        {
            tempCell = Instantiate(PrefabCharCell);
            tempCell.transform.SetParent(GridCellPos,false);
            SomeMethod(tempCell);
        }  
    }
}

S
Suren, 2018-05-31
Jagaryan @saylar

Why pass selected objects to other methods? it won't solve the problem in any way.
You need to push them into the Sheet, in the loop where you have the creation.
Then you already work with the sheet and you will have both the first and second object, you will search by index and that's it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question