- Home /
Spawn GameObject Above and Below GameObject
I want to have it so when he shoots and hits a fruit, it will spawn a split picture of the fruit above the watermelon and below it. It will destroy the fruit, but I want the split picture to spawn where ever the fruit is because the fruit falls and you try to hit the fruit. So to kinda sum it up, I want it to spawn an object above and below where ever the watermelon is when I shoot. Thank you in advance.
UnityEngine; using System.Collections;
public class attackTrigger : MonoBehaviour {
public GameObject watermelon;
public GameObject strawberry;
public GameObject grape;
public GameObject banana;
public GameObject orange;
public GameObject tomato;
public GameObject pineapple;
public GameObject apple;
public GameObject blueberry;
public GameObject raspberries;
public GameObject pear;
public GameObject blackberries;
public GameObject lemon;
public GameObject watermelon_split;
public GameObject strawberry_split;
public GameObject grapes;
public GameObject banana_split1;
public GameObject banana_split2;
public GameObject orange_split;
public GameObject pineapple_split;
public GameObject apple_split;
public GameObject blueberry_split;
public GameObject raspberry;
public GameObject pear_split;
public GameObject blackberry;
public GameObject lemon_split;
public AudioSource score;
public AudioSource hit1;
public AudioSource hit2;
public AudioSource hit3;
public AudioSource hit4;
public AudioSource hit5;
private gameMaster gm;
private spawnFruit spawnfruit;
public int number = 0;
void Start()
{
gm = GameObject.FindGameObjectWithTag("GameMaster").GetComponent<gameMaster>();
spawnfruit = GameObject.Find("spawnFruit").GetComponent<spawnFruit>();
}
void randomHit()
{
number = Random.Range (1,6);
if(number == 1)
{
hit1.Play ();
}
if(number == 2)
{
hit2.Play ();
}
if(number == 3)
{
hit3.Play ();
}
if(number == 4)
{
hit4.Play ();
}
if(number == 5)
{
hit5.Play ();
}
}
void OnTriggerEnter2D(Collider2D col)
{
if(col.isTrigger != true && col.CompareTag("watermelon"))
{
score.Play ();
gm.points += 1;
Invoke("randomHit", 0f);
Destroy(col.gameObject);
}
if(col.isTrigger != true && col.CompareTag("strawberry"))
{
score.Play ();
gm.points += 1;
Invoke("randomHit", 0f);
Destroy(col.gameObject);
}
if(col.isTrigger != true && col.CompareTag("grapes"))
{
score.Play ();
gm.points += 1;
Invoke("randomHit", 0f);
Destroy(col.gameObject);
}
if(col.isTrigger != true && col.CompareTag("banana"))
{
score.Play ();
gm.points += 1;
Invoke("randomHit", 0f);
Destroy(col.gameObject);
}
if(col.isTrigger != true && col.CompareTag("orange"))
{
score.Play ();
gm.points += 1;
Invoke("randomHit", 0f);
Destroy(col.gameObject);
}
if(col.isTrigger != true && col.CompareTag("tomato"))
{
spawnfruit.fruitHit = true;
}
if(col.isTrigger != true && col.CompareTag("pineapple"))
{
score.Play ();
gm.points += 1;
Invoke("randomHit", 0f);
Destroy(col.gameObject);
}
if(col.isTrigger != true && col.CompareTag("apple"))
{
score.Play ();
gm.points += 1;
Invoke("randomHit", 0f);
Destroy(col.gameObject);
}
if(col.isTrigger != true && col.CompareTag("blueberry"))
{
score.Play ();
gm.points += 1;
Invoke("randomHit", 0f);
Destroy(col.gameObject);
}
if(col.isTrigger != true && col.CompareTag("raspberries"))
{
score.Play ();
gm.points += 1;
Invoke("randomHit", 0f);
Destroy(col.gameObject);
}
if(col.isTrigger != true && col.CompareTag("pear"))
{
score.Play ();
gm.points += 1;
Invoke("randomHit", 0f);
Destroy(col.gameObject);
}
if(col.isTrigger != true && col.CompareTag("blackberries"))
{
score.Play ();
gm.points += 1;
Invoke("randomHit", 0f);
Destroy(col.gameObject);
}
if(col.isTrigger != true && col.CompareTag("lemon"))
{
score.Play ();
gm.points += 1;
Invoke("randomHit", 0f);
Destroy(col.gameObject);
}
}
}
If you are just starting out with Unity we suggest checking out the Learn section, as there exists several tutorials, documentation and live training sessions.
If you need to learn about program$$anonymous$$g then please go through the scripting tutorials.
https://unity3d.com/learn/tutorials/topics/scripting
Specifically, instantiating new objects is covered in part 24 "Instantiate".
Your answer
Follow this Question
Related Questions
Spawn A Set Amount of Objects at Set Location 1 Answer
How to make Mobile Touch Controls work when pressing the left and right side of the screen 1 Answer
Filling array with children of multiple undefined gameobjects? 1 Answer
How do I instantiate a class Object, then store it in an Array of Class Objects in C#? 1 Answer