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 Saint0120 · Jan 20, 2015 at 01:52 AM · instantiatetransformenemygetcomponent

GetComponent for a stored transform.

Hi, Im making a tower defense game but ran into some problems with my tower. When an enemy enters the tower's trigger, it stores their transform. For the tower's bullet that it instantiate, I want to use a GetComponent for the stored enemy transform, and insert it in the bullet's transform.LookAt() so that the bullet will go towards the enemy. However I cant seem to get the the GetComponet to properly access the stored transform within the tower.

Sorry I forgot to add the scripts There are 3 scripts, the first one is attached to the actual tower, all it really does is instantiate the bullet from the attached game object // // // using UnityEngine; using System.Collections;

public class TowerD : MonoBehaviour {

 //The seconds inbetween cannon fire 
 public int Fire_Rate = 1;
 //The prefab used for the Cannon Ammo
 public GameObject Cannon_Ammo;
 //Where the Cannon Ammo comes out
 public Transform Cannon_Open;

 //The enemy target used for the trigger
 GameObject EnemyT;


 void Start()
 {
     StartCoroutine(FIRE());
 }

 void Update()
 {

 }


 void OnTriggerEnter(Collider other)
 {
     if (other.tag == "Enemy") 
     {
         EnemyT = other.gameObject;
     }

 }




 //Allows the Tower to fire then wait between each new fire
 IEnumerator FIRE()
 {
     Instantiate (Cannon_Ammo, Cannon_Open.position, Cannon_Open.rotation);

     yield return new WaitForSeconds(Fire_Rate);

     StartCoroutine(FIRE());
 }

}

// // //

This script is attached to an object that is attached to the tower. It has the main trigger so that when an enemy enters, it stores its transform, and adds it to its own transform.LookAt();

// // //

using UnityEngine; using System.Collections;

public class TargetingD : MonoBehaviour {

 //public List<Transform> targets;  


 public Transform target;
 
 //GetComponent for the Bullet class
 private Bullet bullet;

 void Start () {

 
 }


 void Update() 
 {

     transform.LookAt(target);

 }


 //checks to see if an enemy has activated the trigger. If so, their transform is stored
 void OnTriggerEnter(Collider other)
 {
     //EnemyT = other.gameObject;
     if(other.tag == ("Enemy"))
     {
         target = other.transform;
     }
 }

}

This last script is on the bullet thats instantiated. Its in this script that tries to use the getComponent of the previous script, but doesnt seem to want to work.

// // //

using UnityEngine; using System.Collections;

public class Bullet : MonoBehaviour {

 public float BulletLife = 1.0f;
 public float BulletSpeed = 1.0f;



 public Transform TargetTemp;
 public Transform StoreTarget;
 //GetComponent the TargetingD script
 private TargetingD TargetD;
 


 void Awake()
 {

 }

 void Start()
 {
     //TargetD = transform.Find
 }
 

 void Update()
 {
     //Back up if the get component wont work
     StoreTarget = TargetD.target;
     transform.LookAt(StoreTarget);


     //Target = StoreTarget.GetComponent<TargetingD>();



     //Cuases the bullet to move forward
     transform.Translate(Vector3.forward * BulletSpeed * Time.deltaTime, Space.Self);

     //Debug.Log(Target.EnemyT);
 }



 IEnumerator Life()
 {
     yield return new WaitForSeconds(BulletLife);

     Destroy(gameObject);
 }

 


}

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 getyour411 · Jan 20, 2015 at 01:54 AM 0
Share

Relevant code please

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Endless 3D plane repetition animated by script is not moving 0 Answers

How can I get a GameObject's transform in game and set that to a variable? 1 Answer

Getting instance of an sub object rather than the original's subobject 0 Answers

NavMesh giving jerky like motion 0 Answers

Make object move in a direction depending on where it spawns? (C#) 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