- Home /
How to assign a gameobject to a script that is added dynamically
How to assign a gameobject to a script that is added dynamically? I have this script that after a certain event adds a script to a gameobject. This script needs a target(gameobject). Here are the scripts:
Script that adds EnemyAI to gameobject
using UnityEngine;
using System.Collections;
public class AddingScriptsToSelf : MonoBehaviour {
public GameObject mirror;
private MirrorHealth mirrorHealth;
void Awake () {
mirrorHealth = mirror.GetComponent<MirrorHealth>();
}
void Update () {
if(mirrorHealth == null){
gameObject.AddComponent("EnemyAI");
Destroy(this);
}
}
}
EnemyAI:
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public int maxDistance;
private Transform myTransform;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
maxDistance = 2;
}
// Update is called once per frame
void Update () {
Debug.DrawLine (target.position, myTransform.position, Color.green);
//Look at target
myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation (target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if(Vector3.Distance(target.position, myTransform.position) > maxDistance) {
//move towards target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
}
EnemyAI tutorial: https://www.youtube.com/watch?v=O8-oZfi4utY
In your Awake() method, you should cache the transform by finding it. The simplest way , though not the most optimized is to use the GameObejct.FindWithTag("YourGameObjectTag"); so it would run like so...
void Awake() { //your other code target = GameObject.FindGameObjectWithTag("TheObjectsTagYouWantToCacheToThisTarget"); //your target now has a copy of that object referenced to it }
I see you have used this code before, and with it beign in Awake, it will assign the gameobject before the script runs the first time ensuring it wont return null on it, in case you wanted to check for it before assigning it in etc. (error checking/testing stuff)
Take care bud, hope that helps some Gruffy
Answer by Hjalte · Apr 05, 2014 at 09:35 PM
Thanks for the help but i couldnt get it to work. But if anyone else is stock with this then heres how i got it to work: Attach EnemyAI to my gameobject in the inspector and then setting the public gameobject normally then disabling the script and activating it through the first script(AddingScriptsToSelf) heres the code as it is now: using UnityEngine; using System.Collections;
public class AddingScriptsToSelf : MonoBehaviour {
public GameObject mirror;
private MirrorHealth mirrorHealth;
void Awake () {
mirrorHealth = mirror.GetComponent<MirrorHealth>();
}
void Update () {
if(mirrorHealth == null){
gameObject.GetComponent<EnemyAI>().enabled = true;
Destroy(this);
}
}
}
Your answer
Follow this Question
Related Questions
shrinking objects 1 Answer
How to make a public game object array inside an array (or 2D array) that can drag and drop to fill? 1 Answer
I wanted to make the game over if the player rotation is some value 1 Answer
How to make an object visible only after grabbing other object? 0 Answers
Proximity checker 1 Answer