Hello I am working on a typing game where the player enters a string into an inputfield and once the string is submitted, the player will find an enemy of that name and shoot at it (enemies randomly spawn with a name that is a word from a text file)
In my script for shooting the enemies I use a GameObject.Find function but it always returns null.
Here is my script :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class KillTarget : MonoBehaviour {
public bool isSent;
public GameObject shot;
public string nameToFind;
public float speed;
private Quaternion rot;
public Transform shotspawn;
public Transform turret;
void Start () {
isSent = false;
}
void Update () {
if (isSent == true) {
nameToFind = TextInput.newText;
GameObject go = GameObject.Find(nameToFind.ToLower());
if (go != null)
{
if(go.GetComponent<SpriteRenderer>().isVisible)
{
Debug.Log("The Target is : " + go.name);
Explode(go);
}
else
{
Miss (go);
}
}
else
{
Miss (go);
}
}
}
void Explode(GameObject go)
{
rot = Quaternion.LookRotation (turret.position - go.transform.position, Vector3.forward);
turret.rotation = rot;
turret.eulerAngles = new Vector3 (0, 0, turret.eulerAngles.z);
GameObject clone = Instantiate (shot, shotspawn.position,Quaternion.identity) as GameObject;
clone.GetComponent<Rigidbody2D> ().velocity = clone.transform.up * speed;
isSent = false;
}
void Miss(GameObject go)
{
Destroy (go);
Debug.Log ("No Target");
isSent = false;
}
}
the newText string is from the text input and isSent is also from the text input
Any Ideas on how I could make this work?