I have two scripts, on one game object (one acting as the GUI one doing everything else) then I also have a scripts acting as the AI for a sword. I’m getting an error on the sword AI script claiming I haven’t set an instance of the object the sword is supposed to target.
Sword AI script:
using UnityEngine;
using System.Collections;
public class SwordAI : MonoBehaviour {
public GameObject enemy;
public int speed = 2;
public Battle_Character battle_character;
// Use this for initialization
void Start () {
enemy = gameObject.GetComponent<Battle_Character>().Enemy_one;
}
// Update is called once per frame
void Update () {
transform.position = Vector3.MoveTowards (transform.position, enemy.transform.position, speed);
}
}
GUI Script:
using UnityEngine;
using System.Collections;
public class BattleGUI : MonoBehaviour {
public float ScreenHeight;
public float ScreenWidth;
public bool sword = false;
private bool targeting = false;
// Use this for initialization
void Start () {
ScreenHeight = Screen.height;
ScreenWidth = Screen.width;
}
// Update is called once per frame
void Update () {
}
void OnGUI () { //Bigger is farther right and down smaller the # smaller the button
if(targeting != true){
if(GUI.Button (new Rect (ScreenWidth * (0.12f), ScreenHeight * (0.78f), ScreenWidth * (0.1f), ScreenHeight * (0.2f)), "Target")) {
targeting = true;
}
}
if (targeting == true) {
if (GUI.Button (new Rect (ScreenWidth * (0.025f), ScreenHeight * (0.78f), ScreenWidth * (0.12f), ScreenHeight * (0.2f)), "Dagger")) {
}
if (GUI.Button (new Rect (ScreenWidth * (0.16f), ScreenHeight * (0.78f), ScreenWidth * (0.12f), ScreenHeight * (0.2f)), "Sword")) {
sword = true;
}
if (GUI.Button (new Rect (ScreenWidth * (0.295f), ScreenHeight * (0.78f), ScreenWidth * (0.12f), ScreenHeight * (0.2f)), "Bow")) {
}
if (GUI.Button (new Rect (ScreenWidth * (0.43f), ScreenHeight * (0.78f), ScreenWidth * (0.12f), ScreenHeight * (0.2f)), "Axe")) {
}
if (GUI.Button (new Rect (ScreenWidth * (0.565f), ScreenHeight * (0.78f), ScreenWidth * (0.12f), ScreenHeight * (0.2f)), "Fire")) {
}
}
}
}
Everything else script:
using UnityEngine;
using System.Collections;
public class Battle_Character : MonoBehaviour {
public BattleGUI battlegui;
public GameObject sword;
public GameObject hand;
public GameObject Enemy_one;
// Use this for initialization
void Start () {
battlegui = gameObject.GetComponent<BattleGUI>();
}
// Update is called once per frame
void Update () {
if(battlegui.sword == true){
Rigidbody clone;
clone = Instantiate(sword, hand.transform.position, sword.transform.rotation) as Rigidbody;
battlegui.sword = false;
}
}
}
It says the problem is in this line of the sword AI script:
enemy = gameObject.GetComponent<Battle_Character>().Enemy_one;
Any ideas what I’m doing wrong?