Hello.
Im trying to find objects after they are instantiated.
So here are the function that Create Items, DeleteThem
public void CreateItems()
{
numberOfObjects = objectScript.ItemsNumber;
string letter = objectScript.ItemsColor;
//impar
if(numberOfObjects % 2 != 0)
{
iterator = -numberOfObjects +1;
offsetX = 2f;
int j = 0;
for(i = iterator; i < numberOfObjects ; i += offsetX)
{
ReadAString(letter,j,i , yPosition);
j++;
}
}
//par
else
{
iterator = - numberOfObjects +1;
offsetX = 2.0f;
if(numberOfObjects !=0 )
{
int j = 0;
for(i = iterator; i < numberOfObjects ; i += offsetX)
{
ReadAString(letter, j, i, yPosition);
j++;
}
}
}
// Check for exceptions case
if(numberOfObjects == 0)
{
return;
}
}
public void DeleteItems()
{
// Destroy any Items when The function is Calleds
foreach (GameObject i in prefabs)
Destroy (i);
// Clear List
prefabs.Clear ();
}
public void ReadAString(string letter,int j, float i, float yPosition)
{
// Get the specific Letter
switch(letter[j])
{
case 'B':
cube = Item_bbg;
break;
case 'G':
cube = Item_gbg;
break;
case 'O':
cube = Item_obg;
break;
case 'P':
cube = Item_pbg;
break;
case 'R':
cube = Item_rbg;
break;
case 'Y':
cube = Item_ybg;
break;
default: break;
}
//Create a Clone of Item with the specific Prefab and Color
GameObject clone = Instantiate (cube, new Vector3 (i , yPosition, -1), Quaternion.identity) as GameObject;
//Add the Clone in a list for a further use
prefabs.Add(clone);
}
Now I want to find an Item (Clone) that was instantiated.And Find it’s child
This are variables for Items
[HideInInspector]
public Vector3 blueScale;
[HideInInspector]
public Vector3 orangeScale;
[HideInInspector]
public Vector3 yellowScale;
[HideInInspector]
public Vector3 purpleScale;
[HideInInspector]
public Vector3 greenScale;
[HideInInspector]
public Vector3 redScale;
public float blueEnergy;
public float greeEnergy;
public float orangeEnergy;
public float purpleEnergy;
public float redEnergy;
public float yellowEnergy;
[HideInInspector]
public Transform blue;
[HideInInspector]
public Transform green;
[HideInInspector]
public Transform orange;
[HideInInspector]
public Transform purple;
[HideInInspector]
public Transform red;
[HideInInspector]
public Transform yellow;
public GameObject _blue;
public GameObject _green;
public GameObject _orange;
public GameObject _purple;
public GameObject _red;
public GameObject _yellow;
I made an Function
public void FindItemsObjects(GameObject _Blue, Transform Blue, Vector3 BlueScale, GameObject _Green, Transform Green, Vector3 GreenScale,
GameObject _Orange, Transform Orange, Vector3 OrangeScale, GameObject _Purple, Transform Purple, Vector3 PurpleScale,
GameObject _Red, Transform Red, Vector3 RedScale, GameObject _Yellow, Transform Yellow, Vector3 YellowScale)
{
//energyBarSprite = bluePrefab.GetComponent<SpriteRenderer> ();
_Blue = GameObject.Find ("Item_bbg(Clone)");
Blue = FindChild (_Blue, "line");
Debug.Log (Blue);
_Green = GameObject.Find ("Item_gbg(Clone)");
Green = FindChild (_Green, "line");
_Orange = GameObject.Find ("Item_obg(Clone)");
Orange = FindChild (_Orange, "line");
_Purple = GameObject.Find ("Item_pbg(Clone)");
Purple = FindChild (_Purple, "line");
_Red = GameObject.Find ("Item_rbg(Clone)");
Red = FindChild (_Red, "line");
_Yellow = GameObject.Find ("Item_ybg(Clone)");
Yellow = FindChild (_Yellow, "line");
BlueScale = Blue.localScale;
OrangeScale = Orange.localScale;
YellowScale = Yellow.localScale;
PurpleScale = Purple.localScale;
GreenScale = Green.localScale;
RedScale = Red.localScale;
}
And Now I call it.
if(GUI.Button(new Rect(10,120,100,50), "New Level"))
{
;
autoObject.DeleteItems();
autoObject.CreateItems();
FindItemsObjects(_blue, blue, blueScale, _green, green, greenScale, _orange, orange, orangeScale,
_purple, purple, purpleScale, _red, red, redScale, _yellow, yellow, yellowScale);
}
Ok if Im putting an Debug.Log in the FindItemsObject
like Debug.Log (Blue);
it will find me the coresponding object in the game. That’s good.
Now if I want to use this variable from another script it says to me that its null
UpdateEnergyBar (menuScript.blue, blueEnergy, blueScale)
and this is the Update Energy bar Function
public void UpdateEnergyBar (Transform energySprite, float currentEnergy, Vector3 energyScale)
{
Debug.Log ("EnergySprite " + energySprite.name);
energySprite.transform.localScale = new Vector2 (energyScale.x + currentEnergy , 1);
Debug.Log ("energySprite scale on x axes " + energySprite.transform.localScale.x);
}
What Im doing here is Create Items and Find their Child (for the first time it working)
Then When I pres New Level it must delete the old Items, Create new Items and Find Again their children, but here is the problem they are not found, because the compiler says me that the transforms was deleted , that means that was deleted when I Delete the Items.
But what is not normal for me is that After I delete Im creating again the Items and then Im searching again for his child but I dont know why it’s not showing, Please help me :d