Sprite.Create c#

Can anyone expand on this definition a bit? I have a folder named Textures with a texture called health inside of it (all of this under the asset folder). How would I call this? And how do I find out what the rect should be? Any help would be much appreciated!

Could you first tell what you are trying to do? Is it necessary to to do this via code? Why not just use the Sprite Editor (In the import settings of the texture) to define the Sprite?

Sprites are 2D graphic objects used for characters, props, projectiles and other elments of 2D gameplay. The graphics are obtained from bitmap images - Texture2D. The Sprite class primarily identifies the section of the image that should be used for a specific sprite. This information can then be used by a SpriteRenderer component on a GameObject to actually display the graphic.

The best explanation is to get you a picture… Do a search in google for “Sprite Sheet” This will explain what all this means much faster.

I am randomly generating power ups and spawning them, so I believe this must be done via code. The powerup generated can be one of three powerups.

You don’t have to do it 100% through code. Prefabs would be better suited. Create a prefab for each power up and randomly spawn one of the three.

You are probably, right, however I can’t find anything that would do this on the reference. What would I call to do this? Is there a reference I missed somewhere?

Simple way… Make 3 objects, then put this code into a script someplace.

public GameObject[] powerUpTemplates;

void Start(){
	foreach(GameObject template in powerUpTemplates){
		template.SetActive(false);
	}
}

GameObject CreatePowerUp(Vector3 location){
	if(powerUpTemplates.Length == 0) return; // return if you have no template objects
	GameObject template = powerUpTemplates[Mathf.Floor(Random.value * powerUpTemplates.Length)]; // get a random power up.
	
	// create the power up
	GameObject powerUp = (GameObject)Instantiate(template, location, Quaternion.identity);
	powerUp.transform.Rotate(0,Random.value * 360,0);
	powerUp.SetActive(true); // remember to set the powerUp to active.
	
	// return it, just in case you want to do anything with it.
	return powerUp;
}

Read it, look things up and understand what’s going on with each line. That is the best way to learn.

Create the prefab via the inspector then call it with Instantiate whenever you want the powerup to drop:

GameObject myPowerupPrefab;
Instantiate(myPowerupPrefab, transform.position, Quaternion.identity);

or tie powerups to a random call to spawn random powerups:

GameObject myPowerupPrefab1;
GameObject myPowerupPrefab2;
GameObject myPowerupPrefab3;

int randomPowerup = Random.Range(0, 3);
switch(randomPowerup) {
     case 0:
          Instantiate(myPowerupPrefab1, transform.position, Quaternion.identity);
          break;
     case 1:
          Instantiate(myPowerupPrefab2, transform.position, Quaternion.identity);
          break;
     case 2:
          Instantiate(myPowerupPrefab3, transform.position, Quaternion.identity);
          break;
}

You could also add the powerups to an array of GameObjects and Instantiate a random powerup from the array:

GameObject[] myPowerUps;
int randomPowerup = Random.Range(0, myPowerUps.length);

Using:

void CreateNewPowerup(object source, ElapsedEventArgs e)
	{
		//create new powerup
		switch (newPower)
		{
			case 0:
			Instantiate(GameObject pre_HealthPowerup);
			break;
			case 1:
			break;
			case 2:
			break;
		}
	}

I get an error of CS1525: Unexpected symbol ‘pre_HealthPowerup’.

Instantiate may not have a definition in it.

void CreateNewPowerup(int newPower, Vector3 position){
	GameObject powerup;
	switch (newPower)
	{
	case 0:
		powerup = Instantiate(pre_HealthPowerup, position, Quaternion.identity);
	break;
	case 1:
	break;
	case 2:
	break;
	}
}

It still gives me an error CS1525 cannot find pre_HealthPowerup. Is this because I have scripts and prefabs in different folders? I tried this code:

using UnityEngine;
using System.Collections;
using System.Timers;

public class Random_Powerup_Generation : MonoBehaviour 
{
	private static Timer PowerupTimer;
	private int newPower;

	// Use this for initialization
	void Start () 
	{
		//randomize timer to int between 15000 and 45000
		double powerupInterval = Random.Range(15000,45000);
		//create timer
		PowerupTimer = new Timer(powerupInterval);
		//create the event for the timer
		PowerupTimer.Elapsed += new ElapsedEventHandler (CreateNewPowerup);
		//start timer
		PowerupTimer.Start();
		//Make sure it resets
		PowerupTimer.AutoReset = true;
	}

	// Update is called once per frame
	void Update ()
	{
		//random powerup
		newPower = 0; //Random.Range(0,2);
	}
	
	void CreateNewPowerup(object source, ElapsedEventArgs e)
	{
		//create new powerup
		GameObject healthPower = ../Prefabs/pre_HealthPowerup;
		
		switch (newPower)
		{
			case 0:
				Instantiate (healthPower);
				break;
			case 1:
			break;
			case 2:
			break;
		}
	}
}

But I now get an “unexpected symbol ‘.’” error. How can I tell Unity where my prefab is?

This doc helped explain it a bit more, and I changed the variable from a private to a public and dragged the prefab into the variable through the inspector.

Sigh… The whole thing is that you are having a breakdown on how to get started. All of your code is very linear and is hurting the way you are approaching the situation. Lets start by saying that building a timer and letting the timer do the work is pretty much an absurd method of tackling this.

You would do well to learn that when you make everything simple for the user, you make it simple to visualize and execute. (the user being you at the moment)

By definition, you want to have a group of power ups as templates, and a group of locations that those power ups can occupy. You will also need a matching group to hold if a power up is there or not.

Next you want to go through the available locations (where there is not a power up) and add one to one of those locations, while updating the match list.

The whole thing simplifies it’s self when you get rid of switches, and trying to match 15 different variables up to one cause.

To simplify this, you should simply create a game object and attach your power up types to it, and a game object and attach the locations to that.

This gives you a very visual sense of where everything is, and what it looks like.

Next you simply want your code to manage those objects, nothing more. There is not extra timers, no switches and cases, just find one item, find one spot, and put it there.

And yes, I did it, and it works perfectly.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Random_Powerup_Generator : MonoBehaviour
{
	public Transform powerUps; // children of this object are PowerUps.
	public Transform powerUpLocations; // children of this object are PowerUp locations. * use blocks
	public Vector2 interval = new Vector2(15, 45); // seconds to the next power up.
	public bool useRandomRotation = true;
	
	private Transform[] currentPowerups; // hold all the current powerups.
	private float nextPowerUp = 0; // time the next power up will start.
	private Transform powerUpCollector; // used to keep it clean.
	
	void Start ()
	{
		// disable all the children of the power up templates.
		for (int i=0; i<powerUps.childCount; i++) {
			powerUps.GetChild(i).gameObject.SetActive(false);
		}

		// disable all the children of the power up locations.
		for (int i=0; i<powerUpLocations.childCount; i++) {
			powerUpLocations.GetChild(i).gameObject.SetActive(false);
		}

		powerUpCollector = (new GameObject()).transform;
		powerUpCollector.name = "PowerUpCollector";
		currentPowerups = new Transform[powerUpLocations.childCount];
		CreateAllPowerUps();
	}
	
	void Update(){
		if(Time.time > nextPowerUp) CreatePowerUp();
	}
	
	void CreateAllPowerUps(){
		while(CreatePowerUp()){}
	}
	
	bool CreatePowerUp(){
		// update the next powerup time.
		nextPowerUp = Time.time + Random.Range(interval.x, interval.y);
		
		List<int> available = new List<int>();
		for(int i=0; i<currentPowerups.Length; i++){
			if(currentPowerups[i] == null) available.Add(i);
		}
		
		if(available.Count == 0) return false; // cannot create a power up, because no space exists for one.
		
		// get location
		int rndIndex = (int)(Random.value * available.Count);
		int locationIndex = available[rndIndex];
		Transform location = powerUpLocations.GetChild(locationIndex); // get a random location from available

		// get a random power up as a GameObject.
		int powerUpIndex = (int)(Random.value * powerUps.childCount);
		GameObject rndPowerUp = powerUps.GetChild(powerUpIndex).gameObject;

		// create the power up
		GameObject powerUp = (GameObject)Instantiate(rndPowerUp, location.position, Quaternion.identity);
		if(useRandomRotation) powerUp.transform.Rotate(0,Random.value * 360, 0);
		powerUp.SetActive (true);
		
		// set the current powerUp location to the new power up and clean up.
		currentPowerups[locationIndex] = powerUp.transform;
		powerUp.transform.parent = powerUpCollector;
			
		return true; // yes we created a power up.
	}
}

You can use it, forget it and ask the same questions again and again, or use it, learn from it and be better for it. Your choice.