- Home /
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);
}
}
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?
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.
public GameObject CurrentTower;
Target = CurrentTower.GetComponent();
void OnTriggerStay(Collider other) { CurrentTower = other.gameObject; }
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; }
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
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613181532im_/https://answers.unity.com/themes/thub/images/avi.jpg)