So my game with have 4 spawners. One for each cardinal direction. They spawn cubes they should move toawrds the center. The spawners are different in that I have a string so I can name then North, South,etc. When I spawn the cubes I need a velocity based on the string. I need the cube to know which spawner it came from. I don’t know how to do this. I think I need to call the spawn script from the script thats on the spawned cube?
using UnityEngine;
using System.Collections;
public class directionalSpawner : MonoBehaviour {
public string Direction;
public GameObject Cube;
public Vector2 velo;
private string key;
// Use this for initialization
void Start () {
switch (Direction)
{
case "North":
velo = new Vector2(0.0f,-10.0f);
key = "W";
break;
case "South":
velo = new Vector2(0.0f, 10.0f);
key = "S";
break;
case "East":
velo = new Vector2(-10.0f, 0.0f);
key = "D";
break;
case "West":
velo = new Vector2(10.0f, 0.0f);
key = "A";
break;
}
StartCoroutine (Spawn ());
}
// Update is called once per frame
void FixedUpdate () {
}
IEnumerator Spawn(){
while (Input.GetKeyDown(key)) {
Vector3 spawnPosition = new Vector3 (gameObject.transform.position.x, gameObject.transform.position.y, 0.0f);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (Cube, spawnPosition, spawnRotation);
Cube.rigidbody2D.velocity = velo;
yield return new WaitForSeconds (1.0f);
}
}
}