- Home /
(C#)Tracking Instantiated Objects Globally
I think I am misunderstanding the difference/relationship between GameObject and Transform types.
Right now, I have a spawner prefab that uses the following code to spawn enemies:
public void spawnEnemy()
{
var gameController = GameObject.FindWithTag ("GameController");
if(cooldown < 0)
{
//generate a random enemy from the enemy List
int rand = Random.Range(0,enemyList.Length);
//spawn the enemy
var enemy = Instantiate(enemyList[rand],transform.position, Quaternion.identity) as Transform;
//add the enemy to the global spawned enemy list in GameScript.cs
gameController.GetComponent<GameScript>().spawnedEnemies.Add(enemy);
//get the associated cooldown
cooldown = cooldownList[rand];
}
cooldown -= Time.deltaTime;
}
Additionally, I have an invisible GameController that is tracking the spawned enemies (List spawnedEnemies).
Which brings me to my issue: I have a weapon in the game that is essentially a seeking missile. I call the following function:
private void acquireTarget()
{
//find the game controller
var gameController = GameObject.FindWithTag ("GameController");
//if there are untargeted enemies
if(!gameController.GetComponent<GameScript>().noTargets)
{
lockedTarget = gameController.GetComponent<GameScript>().getTarget;
}
}
getTarget basically builds a new List of all existing objects who have the bool isTargeted set to false (essentially populating the list with eligible targets) and then returns one at random.
I then call the following two lines of code at the end of my Update function to attempt to move the missile:
transform.position = Vector3.MoveTowards (transform.position, lockedTarget.transform.position, .03f);
transform.LookAt (lockedTarget.transform);
What actually happens is that the missile object stops drawing on screen, and I am thrown the following error in the console:
UnassignedReferenceException: The variable lockedTarget of 'MissileBehavior' has not been assigned. You probably need to assign the lockedTarget variable of the MissileBehavior script in the inspector. UnityEngine.Component.get_transform () (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineComponent.cs:19) MissileBehavior.Update () (at Assets/Scripts/MissileBehavior.cs:41)
Line 41 is the Vector3.MoveTowards. The problem is, if I inspect the missile object, it shows as having Locked Target assigned as pinwheel_enemy(clone)(Transform). So I'm not sure what's going on.
With this chopped up snapshot of your code, it is hard to figure out where things are going wrong. Usually I appreciate someone isolating the suspect code areas, but in this case, I think we need to see the entire script. If it is larege, you can use pastebin
With the following example, why are you using type var
?
var enemy = Instantiate(enemyList[rand],transform.position, Quaternion.identity) as Transform;
What type is lockedTarget
? Can we see the code for getTarget
?
Sure can!
I'm using var mostly because I am still working at understanding what types are passed around by Unity. I realize var is really not optimal when you can actually strongly type it ins$$anonymous$$d. So I will admit to it being really bad form (and plan on fixing it once I "get" it better)
lockedTarget is set to be a public Transform in the same script as the mentioned Update function.
Here's the code for getTarget:
public Transform getTarget
{
get
{
List<Transform> targets = new List<Transform>();
foreach(Transform elem in spawnedEnemies)
{
if(!elem.GetComponent<HealthScript>().target)
{
targets.Add (elem);
}
}
return targets[Random.Range (0,targets.Count)];
}
}
This is my first foray into C# (C++ was my first language) so I'm fumbling a bit at times to do things I normally would do. $$anonymous$$y thought process for lockedTarget was to try give it a reference to a spawned object to get the missile to it, and the OnTriggerEnter2D code would handle the rest for me (it does for other weapon types). I just haven't figured out how to get that reference set I guess.
Answer by Nephxio · Dec 28, 2013 at 02:14 AM
Thanks guys, this answered a ton of my questions, so going to chew on this a while and see if I can get it working (and mark this answered)
Answer by Roland1234 · Dec 26, 2013 at 07:30 PM
From what I can tell based off the code snippets - if you're getting an UnassignedReferenceException but when you inspect the object you actually see an assigned reference, it seems reasonable to me to assume that you are assigning the reference a frame too late. Try replacing the 2 lines in your missile's Update method with this:
if(lockedTarget != null)
{
transform.position = Vector3.MoveTowards (transform.position, lockedTarget.transform.position, .03f);
transform.LookAt (lockedTarget.transform);
}
else
{
Debug.LogError("lockedTarget is null!");
}
And see if you get the error message logged to the console. If so, then you'll have to go into more detail regarding when you call your acquireTarget() method and whatever else might actually be happening. Posting the entire missile MonoBehaviour would help.
I went ahead and put the 3 relevant scripts in pastebin here:
SpawnerScript.cs http://pastebin.com/4y2DdJJu (spawns enemies & adds them to the global array)
GameScript.cs http://pastebin.com/PqvU0Jwa (attached to an invisible object)
$$anonymous$$issileBehavior.cs http://pastebin.com/i6A6HXrp (attached to $$anonymous$$issile Prefab)
It is returning lockedTarget is null in the console as well.
The acquireTarget method is checking your GameScript.noTargets property, which is likely returning true and causing no target to be acquired. Looking at the property I see you are returning true if any spawned enemy has a HealthScript.target property that is true, which looks to me like you're saying "There are no targets if any spawned enemy has been targetted," which I'm pretty sure is a mistake.
I think you meant something like this:
public bool noTargets
{
get
{
foreach(Transform elem in spawnedEnemies)
{
if(!elem.GetComponent<HealthScript>().target)
{
return false;
}
}
return true;
}
}
Still, I can't find any reason why you would be seeing Locked Target assigned in the inspector while getting the error, can you verify and consistently reproduce this?
Answer by KellyThomas · Dec 27, 2013 at 02:41 AM
acquireTarget()
is not guaranteed to find a target:
private void acquireTarget() {
//find the game controller
var gameController = GameObject.FindWithTag ("GameController");
//if there are untargeted enemies
if(!gameController.GetComponent<GameScript>().noTargets) {
lockedTarget = gameController.GetComponent<GameScript>().getTarget;
hasTarget = true;
}
}
If noTargets
return true then lockedTarget
will remain unchanged. You will need any code that follows your call to acquireTarget()
to handle this situation gracefully.
On a more cosmetic note here a couple of changes that could be made to tighten up the typing:
// SpawnerScrpt, line 39 Instantiate has a generic form.
//var enemy = Instantiate(enemyList[rand],transform.position, Quaternion.identity) as Transform;
Transform enemy = Instantiate<Transform>(enemyList[rand],transform.position, Quaternion.identity);
// MissileBehavior, line 41 lockedTarget is already of type Transform
//transform.position = Vector3.MoveTowards (transform.position, lockedTarget.transform.position, .03f);
transform.position = Vector3.MoveTowards (transform.position, lockedTarget.position, .03f);
I had considered that with lockedTarget, that it needed a default target (preferrably one I can check in that if statement) but I'm not sure how to do it. The obvious choice to me is GameController, but it appears that objects placed in the scene via Unitye, even if tagged, are not easily converted to Transforms. Am I missing something obvious?
Well if you just want it to shoot forward this should do it:
transform.position += transform.forward * 0.3f;
But it will rely of have an appropriate rotation.
We haven't seen the code to actually fire these missiles. As a worst case scenario you could add something like this to $$anonymous$$issileBehavior
's Start()
method:
GameObject player = GameObject.Find("Player");
if (player) {
transform.rotation = player.transform.rotation;
}
However running GameObject.Find()
this often is bad practice. You can probably pass an appropriate rotation to your Instantiate()
call without having each missile search out the player GameObject.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
An OS design issue: File types associated with their appropriate programs 1 Answer
How can I Instantiate two new GameObjects at the same time? (Attempting Asteroids style game) 2 Answers
How to Spawn an Object in between Randomly Spawned Objects with no Overlapping? 0 Answers