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 26, 2015 at 04:38 AM · getcomponentlookattower defensetower

Get Component gets the wrong component

Hello, the bullet script for my tower defense game uses GetComponent to get the tranform of the enemy that was triggered by the tower and uses that for transform.LookAt so it can go towards that enemy that triggered the tower.

Currently the problem is that each new bullet created by each tower gets the component of the first tower within the scene and not from the tower that instantiated the bullet

How can I set it up so that the bullet can get the component from the tower that instantiated it, or get the component from the closest tower

Here are the two scripts. The first has the trigger so that when an enemy triggers it, it grabs its transform and stores it. The second script is the bullet script and uses getcomponent to grab the transform stored from the first script and use it for its transform.LookAt

//

//

//first script

using UnityEngine; using System.Collections;

public class TowerOpeningScript : MonoBehaviour {

 public Transform target;  //Store the Enemy's tranform for LookAt()
 private TowerBullet bullet; //GetComponent for the Bullet class


//***

 //Put this script on the opening of where the tower fires its bullets.
 //The TowerBullet script relies on this script for its GetComponent of the target.
 //******************************************************************


 void Update() 
 {
     transform.LookAt(target);
 }


 //checks to see if an enemy has activated the trigger. If so, their transform is stored
 void OnTriggerStay(Collider other)
 {

     if(other.tag == ("Enemy"))
     {
         target = other.transform;
     }
 }

}

//Second Script

//

//

using UnityEngine; using System.Collections;

public class TowerBullet : MonoBehaviour {

 public float BulletLife = 1.0f;    //How long the bullet lives before destoyed
 public float BulletSpeed = 1.0f;   //How fast the bullet moves
 public Transform StoreTarget;      //Store the Enemy's tranform for LookAt()
 private TowerOpeningScript Target; //GetComponent the TargetingOpeningcript
 
 //***********************************
 //This script goes on the bullet the tower uses
 //Uses a GetComponent for the TowerOpeningScript(assuming I didnt combine it with the TowerScript) and get the tranform that was triggered within that script for this one
 //***********************************

 void Awake()
 {

 }

 void Start()
 {
     //This is the reference for the TowerOpeningScript so that it knows what to look for
     //Currently the problem is that each new bullet created by each tower gets the component of the first tower within the scene and not from the tower that instantiated the bullet
     //Need to figure out a way so that it uses GetComponent on its nearest tower
     //Or use a list to scroll through, may need to ask for help on this one
     Target = GameObject.FindGameObjectWithTag ("TowerTag").GetComponent<TowerOpeningScript>();

     StartCoroutine (Life ());

 }
 

 void Update()
 {
     StoreTarget = Target.target;
     transform.LookAt(StoreTarget); 

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

 }

 //Checks to see how long the bullet lives before destroyed
 IEnumerator Life()
 {
     yield return new WaitForSeconds(BulletLife);
     Destroy(gameObject);
 }

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Derek-Wong · Jan 26, 2015 at 07:23 AM

how about create a GameObject tower variable in the bullet script, and set the value to the tower which instantiate the bullet in the tower script?

Comment
Add comment · Show 7 · 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
avatar image Saint0120 · Jan 26, 2015 at 08:44 AM 0
Share

I feel that gets me closer to my answer, but didnt work. I created the new GameObject variable and OnTriggerStay that triggers when it leaves the tower and store the gameobject of that tower for the new gameobject variable and use that for my getcomponent, but still doesnt work, here are snips of the new code.

avatar image Saint0120 · Jan 26, 2015 at 08:45 AM 0
Share

public GameObject CurrentTower;

Target = CurrentTower.GetComponent();

void OnTriggerStay(Collider other) { CurrentTower = other.gameObject; }

avatar image Saint0120 · Jan 26, 2015 at 09:32 AM 0
Share

I feel like your on the right track, but Im still running into problems. That still gives me the problem that the bullet grabs the value of the very first tower created within the scene.

I got an idea though from your suggestion. I tried making a OnTriggerEnter in the bullet script, and since each bullet collides once it leaves the tower that created it, it should be able to detect that, however Im still getting trouble.

//I added new GameObject variable like you suggested public GameObject CurrentTower;

//Below is the old GetComponent //Target = GameObject.FindGameObjectWithTag ("TowerTag").GetComponent(); //Below is the new GetComponent thats suppose reference the game object thats triggered Target = CurrentTower.GetComponent();

//When the bullet is instantiated, the tower has trigger this, so it should grab the tower as the GameObject void OnTriggerEnter(Collider other) { CurrentTower = other.gameObject; }

avatar image Saint0120 · Jan 26, 2015 at 09:50 AM 0
Share

Ok got it to work! $$anonymous$$y get component was in the void Start where they usually are, however I realized that it cant be be called right away because it hadnt triggered yet, so I put it in update ins$$anonymous$$d, sadly that creates a new problem that Im flooded with errors because theres no component right away, but at least the script is working

avatar image Derek-Wong · Jan 26, 2015 at 10:54 AM 0
Share

well seems you r script work but avtually i cannot understand your game logic. i guess that you are having many towers in your scene and there are many enemies rushing to your base and your towers will shoot them down, am i correct?

my advice on your code is, do not assign the value of the current tower in the bullet script, assign the value when you instantiate it in the towerscript, e.g.

GameObject bulletPrefab = (GameObject)instantiate (bullet);

BulletScript bulletscript = bulletPrefab.GetComponent();

bulletscript.CurrentTower = this.gameObject;

it should works, but it is better for you to explain more on your gameflow, i am afraid that i have misunderstood something about your game.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Showing Tower's range in game 3 Answers

Sendmessage/GetComponent (read variable only) help 1 Answer

Need help with tower defense win level system,need help with tower defense win level system 0 Answers

Make an FPS gun point at the mouse position. 2 Answers

LookAt but using physics AddRelativeTorque 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