0
votes

I'm pretty new to Unity3D and C# so I'm not entirely sure what I'm doing wrong in the following scenario.

In Unity3D I'm attempting to get the value of a variable (target) that is updated when the player moves close enough to the enemy that the script changes target from null to 'enemy' and gets the position and so on and so forth.

The turret that has a script on it for auto-targetting is a child to the main player gameobject.

Whenever I use the following:

targettingScript target;

target = gameObject.GetComponent<targettingScript>();
// or---
target = gameObject.GetComponentInChildren<targettingScript>();

...I can then test with a debug.log that it does pull "null" as it should at start up, but it doesn't seem to update when the turret finds a target and changes target from null.

I've read a few forum posts in various areas that some say you can't do this (pass data from a child component script to another gameobject that is instantiated (IE a missile firing after targetting is completed and then doing a homing-missile style of tracking) and others said that they did a check through all the levels of the gameObject till they found what they wanted, but I'm not understanding what that really means.

My questions are, 1 - is it possible to relay a variable from a child gameobject's component to another instantiated gameobject 2 - if so, how?

I apologize if this is a rather simple concept, but I'm not sure how to troubleshoot it. I'm going to build a new scene and try various gameObjects and child objects and see what I can muster and if I find anything useful I will post it here.

In the mean time, thank you all for your time in reviewing my question.

1
how is the hierarchy of your game objects in the Hierarchy pane in the Unity editor?Roberto
right now it's a bit of a mess since i'm just trying to build some basic mechanics and learn unity all at once. my player is an empty gameobject, with a base 'hull'. the turret is under that as a child and the aiming pieces and firing hard points on the turret are all children and variously branched out under the turret game object. i ended up using findgameobjectwithtag and i seem to have the right variable, just now getting the missile to track it is the next fun part.Nick Willcox

1 Answers

0
votes

sure,

since you instantiate the object run time, you should keep track of it,

public GameObject target;

public void Update() {
   // if we dont have a target (find it).
   if(target == null) {
      target = GameObject.Find("Player");
   }

   // if we found the target take some health each frame.
   if(target != null) {
      playerhealth = player.GetComponent<PlayerHealth>();
      playerhealth.health--;
   }
}

you can of course also add a distance detector to the find.

if(target != null)
if(Vector3.distance(transform.position, target.transform.position) < 5) {
   // target in range
} else {
   // to far away ignore it.
   target = null;
}

The best thing to do when you are instansiating gameobjects runtime you have to keep track of them if you are going to reference them later on, so create a manager that does this, create a empty gameobject, add a MgrScript to it, and have other objects call it as MgrScript.SpawnEnemy() that way we can reference the enemy's from an array or something like that.

If you dont use a manager and your scene is simple and small you can always use tags to reference the gameobjects, since it a bad idea to reference by name when you have a large and complex scene, because you will most likely reference the wrong item.