- Home /
why does my current code line consist next line of code information
I have two script code as givien below Main Problem :) The main problem is that if my gameobject(i mean pycs[] in code) is destroyed why does my code carrys next pycs[(int)information] like this line carry (WhitePieceXData[3] = pycs[3].GetComponent().Get_X_Board_Easy(pycs[3]);) but why does it converts to (WhitePieceXData[3] = pycs[4].GetComponent().Get_X_Board_Easy(pycs[4]);) while game is runing THANKU SO MUCH FOR YOUR TIME GOD HELP THOSE WHO HELP OTHERS I WILL HELP 1 MORE PERSON IF YOU HELP ME THANKU Team,BDS
// first Script
GameObject[] pycs = GameObject.FindGameObjectsWithTag("Pieces").OrderBy(go => go.transform.GetSiblingIndex()).ToArray(); // info white Pieces
object[] WhitePieceXData = new object[16]; //Made a array of the xboard which i want to send from 0-15
WhitePieceXData[0] = pycs[0].GetComponent<DiedChessman>().Get_X_Board_Easy(pycs[0]);
WhitePieceXData[1] = pycs[1].GetComponent<DiedChessman>().Get_X_Board_Easy(pycs[1]);
WhitePieceXData[2] = pycs[2].GetComponent<DiedChessman>().Get_X_Board_Easy(pycs[2]);
WhitePieceXData[3] = pycs[3].GetComponent<DiedChessman>().Get_X_Board_Easy(pycs[3]);
WhitePieceXData[4] = pycs[4].GetComponent<DiedChessman>().Get_X_Board_Easy(pycs[4]);
WhitePieceXData[5] = pycs[5].GetComponent<DiedChessman>().Get_X_Board_Easy(pycs[5]);
WhitePieceXData[6] = pycs[6].GetComponent<DiedChessman>().Get_X_Board_Easy(pycs[6]);
WhitePieceXData[7] = pycs[7].GetComponent<DiedChessman>().Get_X_Board_Easy(pycs[7]);
WhitePieceXData[8] = pycs[8].GetComponent<DiedChessman>().Get_X_Board_Easy(pycs[8]);
WhitePieceXData[9] = pycs[9].GetComponent<DiedChessman>().Get_X_Board_Easy(pycs[9]);
WhitePieceXData[10] = pycs[10].GetComponent<DiedChessman>().Get_X_Board_Easy(pycs[10]);
second script :)
public object Get_X_Board_Easy(GameObject Gobj)
{
if (Gobj.layer != 8)
{
return Gobj.GetComponent<Chessman>().xBoard;
}
else
{
return null;
Gobj.GetComponent<DiedChessman>().IsDied = true;
Debug.Log(Gobj + " , 0" + Gobj.GetComponent<DiedChessman>().IsDied);
}
}
Answer by SamElTerrible · Oct 11, 2021 at 01:27 PM
I think its not actually converting this:
WhitePieceXData[3] = pycs[3].GetComponent<DiedChessman>().Get_X_Board_Easy(pycs[3]);
into this:
WhitePieceXData[3] = pycs[4].GetComponent<DiedChessman>().Get_X_Board_Easy(pycs[4]);
Instead, by destroying pycs[3] you are removing it from the array, and the array is 'moving' pycs[4] up one slot to become pycs[3].
I'm not sure what you're trying to achieve but by the looks of it I think using a dictionary that maps each WhitePieceXData to a pyc might help
ya you are right it is going one slot up but can you please tell me how to use dictionary
From your code it looks like you're mapping one array onto another. Dictionaries use key value pairs, which means that by getting the key you can get the value. Keys and values can be any data type, but keys must be unique. Like I mentioned, I'm not sure what your end goal is so I'm not sure if a dictionary is the right way to go, but it's definitely a very useful tool for you program$$anonymous$$g toolbox. I'm sure there are plenty resources around on dictionaries and how to use them in Unity. You might just have to do a bit of research
ya you are right Dictionarys are realy helpfull to me Can you please also tell me is there any type of array to sort the array like in php sort() - sort arrays in ascending order rsort() - sort arrays in descending order asort() - sort associative arrays in ascending order, according to the value ksort() - sort associative arrays in ascending order, according to the key arsort() - sort associative arrays in descending order, according to the value krsort() - sort associative arrays in descending order, according to the key
if it is can you please tell me what are they and their function or can you please give me a source where i can reak or know more about it my current problem is to how do i sort a array for example i have this below code right now i want to sort it as like as it is in the hirearcy no matter a object destroy or instantiate GameObject[] obj = GameObject.FindGameObjectsWithTag("Pieces").OrderBy(go => go.transform.GetSiblingIndex()).ToArray();
Your answer
Follow this Question
Related Questions
Instantiate an array of gameobjects with a time delay between each 1 Answer
Object to int 1 Answer
Hello, I can I make a int for my coroutine that can check the value of the int every frame. 1 Answer
Vector3.Lerp curving along three destinations 1 Answer
Trying to trigger a function on another script 1 time, with an Int involved. PlsHelp 0 Answers