Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hjalte · Apr 04, 2014 at 10:40 PM · gameobjectgameobjectspublicaddingdynamically

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gruffy · Apr 04, 2014 at 10:46 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
         }
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges