script code to make my apples respawn?

Hi all, sorry im a newbie to Unity, i’ve been trying to figure out how to make my apples respawn. im doing a apple catch game, and i have all the code i need i just cant figure out where to put them. also i am using c#.

sorry and thanks

this is what i have:

using UnityEngine;
using System.Collections;

public class instantiator : MonoBehaviour
{
public Transform apple;

// Use this for initialization
void Start () 
{

}

// Update is called once per frame
void Update () 
{
	
	{

			
	if(Input.GetButtonDown("Fire1"))
				
	animTime -= Time.deltaTime;
	
	if(animTime <= 0.0f)
	{
		currentFrame += 1;
		animTime += 1.0f / animSpeed;
	{
		
		Instantiate (apple, new Vector3(Random.Range(-2.4f, 2.4f), 2.0f, 0f), Quaternion.identity);
		
	
	}

Line 11 opens a statement that is never started? Line 22 also? I don't see animtime declared either

2 Answers

2

You will need to know the position you want to spawn them, a Prefab of the apple, and a gameObject with the script that will spawn the apples.

First choose the positions where you want to spawn the apples, if you want them not to be repetitive create as many Empty GameObjects as you want, make sure they are invisible, place them where the apples should spawn.
Create a prefab for the apple so it can be cloned in the scene.
Then create a GameObject that will hold the Apples Spawing script and add this to your script:

public Transform[] SpawnPositions;
public Prefab ApplePrefab;

Then fill the array in the inspector with the GameObjects you’ve created first.
Now that you have the spawning positions and the prefab hold in a variable, you can proceed to spawn the apples:

The entire script would look like this:

using UnityEngine; 
using System.Collections;

public class instantiator : MonoBehaviour 
{ 
  public Transform[] SpawnPositions;
  public Prefab ApplePrefab;

void Update () 
{
    if(Input.GetButtonDown("Fire1")) 
    animTime -= Time.deltaTime;
 
    if(animTime <= 0.0f)
    {
       currentFrame += 1;
       animTime += 1.0f / animSpeed;
    {       
    int selectedPos = Random.Range(0,SpawnPositions.Length);
    Instantiate(ApplePrefab, SpawnPositions[selectedPos], Quaternion.identity);
    }

If you don’t want to spawn them when the user clicks, but for example spawn one apple every 1 second then:

void Start()
{
  InvokeRepeating("MyFunction", StartDelay,RepeatDelay);
}

void MyFunction()
{
  animTime -= Time.deltaTime;

if(animTime <= 0.0f)
{
   currentFrame += 1;
   animTime += 1.0f / animSpeed;
{       
int selectedPos = Random.Range(0,SpawnPositions.Length);
Instantiate(ApplePrefab, SpawnPositions[selectedPos], Quaternion.identity);
}
}

okay so i made it this: (but then it’s saying lines 1, and 23 has errors)

using UnityEngine;
using System.Collections;

public class instantiator : MonoBehaviour
{
void Start()
{
InvokeRepeating(“MyFunction”, StartDelay,RepeatDelay);}

void MyFunction()
{
	animTime -= Time.deltaTime;

	if(animTime <= 0.0f)
{
		currentFrame += 1;
		animTime += 1.0f / animSpeed;
	
{
int selectedPos = Random.Range(0,SpawnPositions.Length);
Instantiate(ApplePrefab, SpawnPositions[-2.4f, 2.4f], Quaternion.identity);
}

}

Lines 1 and 23 have errors? Erm- count the number of lines in that code...