- Home /
Question by
vonnbla123 · Jul 23, 2015 at 05:25 PM ·
spritesspriterendererresources.load
Change Sprite Image in Runtime
Good day, I'm having a problem here, No error so far but I can't seem to display my desired Sprite Image in my Resources directory.
here's my code
public void Spawner(){
int platformRange = Random.Range(3,5);
float nextPlat = 0f;
float randomDistance = Random.Range(-2f,1f);
float nextPlatY = Random.Range(-5f,-.5f);
float nextTotalPlatWidth = 0f;
for (int i =0; i <= platformRange; i++) {
GameObject platform;
SpriteRenderer rend = platform.GetComponent<SpriteRenderer>();
Debug.Log (platformRange);
if (i == 0) {
platform = plat [0];
platformLocation = "LeftPlat";
platformStyle = "leftedge_";
} else if (i == platformRange){
platform = plat [2];
platformLocation = "RightPlat";
platformStyle = "rightedge_";
}
else{
platform = plat [1];
platformLocation = "MiddlePlat";
platformStyle = "middlesection_";
}
rend.sprite = Resources.Load (platformLocation + "/"+ platformStyle + itemName) as Sprite;
Debug.Log ("POSITION" + transform.position.x + totalPlatWidth + nextPlat + randomDistance);
Vector3 spawnPosition = new Vector3 (transform.position.x + totalPlatWidth + nextPlat + randomDistance, nextPlatY, 0f);
nextPlat = nextPlat + platform.GetComponent<Renderer> ().bounds.extents.x * 2 - .1f;
if (i == 0){
Instantiate(SpawnerObj,new Vector3(spawnPosition.x,-0.0400f,0f),Quaternion.identity);
}
Instantiate (platform, spawnPosition, Quaternion.identity);
}
}
Function Spawner is triggered by a collision.
Comment
Your answer
Follow this Question
Related Questions
Sprites randomly display as horizontal lines 2 Answers
Fixing Sprites in Unity? 0 Answers
how to check presence of sprite on a UI button? 1 Answer