T
T
Tterka2020-10-29 20:10:07
Unity
Tterka, 2020-10-29 20:10:07

How to use one binary array in multiple unity scripts?

I wrote the code, 64 objects are created, I save them in one binary array

[SerializeField] public GameObject[,] plitkmac = new GameObject [8, 8];
.
.
plitkmac[y,x]=inst;

They are all saved and I can use the array in one script. But I need to use ego in other scripts as well.
Tried this way:
GameObject[,] plitkmac = transform.GetComponent<creature>().plitkmac;
 print(plitkmac[1,1].name);

But unity throws an error and visual studio doesn't.

NullReferenceException: Object reference not set to an instance of an object
panel.cretecreateChecker () (at Assets/Board/scripts/panel.cs:15)
panel.Start () (at Assets/Board/scripts/panel.cs:10)

all code
using System;
using UnityEngine;
using UnityEngine.UI;

 public class creature : MonoBehaviour
{
    public GameObject instpref;
    
   [SerializeField] public GameObject[,] plitkmac = new GameObject[8, 8];

    void Start()
    {
        int plas = 0;


        for (int y=0; y< 8; y++ )
        {
            int gpy = -315 + y * 90;
            for (int x=0; x<8; x++)
            {
                int gpx = -315 + x * 90;
                GameObject inst = Instantiate(instpref, transform);
                int collor = ((8 * y + x+plas)%2);
                if (collor == 1)
                {
                    inst.GetComponent<Image>().color = Color.white;
                }
                inst.GetComponent<RectTransform>().anchoredPosition =new Vector2(gpx, gpy);
                plitkmac[y  ,x] = inst;
                inst.name = "panel" + y + x;
                
            }
           plas++;
        }
    }
  

}

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

public class panel : MonoBehaviour
{
  
    void Start()
    {
        cretecreateСhecker();
    }
    public void cretecreateСhecker()
    {
        GameObject[,] plitkmac = transform.GetComponent<creature>().plitkmac;
        print(plitkmac[1,1].name);
    }

and another question, is it possible to make a binary array visible in the inspector, and if so, how?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question