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_