Adding a spawn point
Now that we have our Weapon prefab put together, we are going to need a way to get it to the player. We've already answered this problem, with the chest, in the previous chapter. We will just reuse the Chest
object to also spawn weapons as well.
Open the Chest.cs
script that's to be edited. We will want to add a variable to store a reference to our generated weapon. Add the public Weapon weapon;
variable right under our randomItem
variable. Then, we need to make some adjustments to our Open()
function, which can be seen in Code Snip 6.3:
1 public void Open () { 2 spriteRenderer.sprite = openSprite; 3 4 GameObject toInstantiate; 5 6 if (Random.Range (0, 2) == 1) { 7 randomItem.RandomItemInit (); 8 toInstantiate = randomItem.gameObject; 9 } else { 10 toInstantiate = weapon.gameObject; 11 } 12 GameObject instance = Instantiate (toInstantiate, new Vector3 (transform.position.x, transform.position.y, 0f), Quaternion.identity) as GameObject; 13 instance.transform...