what is wrong with this code !

hi . i have a code like this :

for(i=0;i<=15;i++)
{
	var Length = PrefabArray.length;
	var PrefabIndex:int = Random.Range(0,Length);
	var CObject:GameObject = Instantiate(PrefabArray[PrefabIndex],
	Triggers_.transform.position,Triggers*.transform.rotation);*_

* var SGO:GameObject = PrefabArray[PrefabIndex];*
* CObject.transform.name = SGO.transform.name;*
* var Image:GameObject = GameObject.Find(CObject.transform.GetChild(0).name);*
* Image.transform.name = SGO.transform.name;*
_ /* Error Happen Here /_
_
Image.GetComponent.().sprite = PrefabImageArray[PrefabIndex];_
_ / Error Happen Here /_
_
CObject.transform.position.y += 0.4;_
_
PrefabArray.RemoveAt(PrefabIndex);_
_
PrefabImageArray.RemoveAt(PrefabIndex);_
_
}_
PrefabImageArray is a list of sprite . PrefabArray is a list(array) of prefab . Triggers(also a list) initiate prefabs one by one and random in a loop . there is a 2D sprite game object in prefabs as child and i find them with this code :
GameObject.Find(CObject.transform.GetChild(0).name);
it works well until i add a image ui (4.6) in a canvas ! then this error shows up :
MissingComponentException: There is no ‘SpriteRenderer’ attached to the “8” game object,but a script is trying to access it.
You probably need to add a SpriteRenderer to the game object “8”. Or your script needs to check if the component is attached before using it.
UnityEngine.SpriteRenderer.set_sprite (UnityEngine.Sprite value) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/SpritesBindings.cs:175)
GameController.Start () (at Assets/Scripts/GameController.js:23)
VERY STRANGE
“8” game object is initiated game object and this error happen in first loop (i=0). then i add a if statement as sugestion like this :
_/ Error Happen Here /_
if(Image.GetComponent.())
_
{ Image.GetComponent.().sprite = PrefabImageArray[PrefabIndex]; }_
else { print(Image.GetComponent.().sprite); }
_/ Error Happen Here */
it solve the problem but first sprite don’t change by code and the printed sprite of first initiated sprite (i=0) is none !!!
WHY THIS HAPPEN ??? HEEEEEEEELP_

Basically, it means that your prefab ( or prefabs ) does not have a sprite rendered component. Then you use GetComponent, which returns null, since the component is not there. And then you try to dereference a null pointer, which causes the error. So here are two pieces of advice:

1- To solve the immediate problem, add sprite renderes to your prefabs

2- To prevent future headaches, always check if the result of a call to GetComponent is not null. And always have a Debug statement to help you find the cause of the problem, for example ( in JS ):

var theRenderer:SpriteRendered = Image.GetComponent(SpriteRenderer);

if (theRenderer) // Shorthand for saying "if this is not null"

{
  // Do stuff
}
else

{    
      Debug.LogError("Sample Code: Could not find SpriteRenderer component in object "+Image.name);  
}

Its always a good habit to have sanity checks in code, it reduces debugging times considerably

Hope that helps

I Find Out !

the game object.find make my loop go faster then finding objects and sprites (low frame rate in start function)

SO

i change it to this :

for(i=0;i<=15;i++)
{
	var Length = PrefabArray.length;
	var PrefabIndex:int = Random.Range(0,Length);
	var SGO:GameObject = PrefabArray[PrefabIndex];
	var CObject:GameObject = Instantiate(PrefabArray[PrefabIndex],
	Triggers_.transform.position,Triggers*.transform.rotation);*_

* CObject.transform.position.y += 0.4;*
* CObject.transform.GetChild(0).name = SGO.transform.name;*
* CObject.transform.name = SGO.transform.name;*
* CObject.transform.GetChild(0).GetComponent.().sprite = PrefabImageArray[PrefabIndex];*
* PrefabArray.RemoveAt(PrefabIndex);*
* PrefabImageArray.RemoveAt(PrefabIndex);*
* }*
there is no finding object , just change the sprite renderer immediatly after rename the 2d sprite !