- Home /
Following Object can't find newly Instantiated Object (Solved)
I have 2 objects in my game, a sphere and a cube. The sphere starts at position A and the cube moves towards it using the following script -
#pragma strict
public var victim : Transform;
private var navComponent : NavMeshAgent;
function Start()
{
navComponent = this.transform.GetComponent(NavMeshAgent);
}
function Update()
{
if(victim)
{
navComponent.SetDestination(victim.position);
}
}
The sphere has a script on it that allows it to be destroyed and instantiated at a new location -
#pragma strict
public var resourceSpawnPoints : Transform[]; //Makes a list of transforms in my game
public var randomPos : int; //This denotes that the variable randomNum will be a whole number
public var resourceType : GameObject;
function OnTriggerEnter(thing: Collider)
{
if (thing.tag == "Harvester")
{
ResourceRespawn();
}
}
function ResourceRespawn()
{
randomPos = Random.Range(0,4); // generates random number between 0 and 3.
transform.position = resourceSpawnPoints[randomPos].transform.position;
//Assigns the number generated in the previous line to the 'spawnPoints' variable
//Which then dictates which spawnPoint(location) is used (spawnPoint1, spawnPoint2, spawnPoint3)
Instantiate(resourceType, resourceSpawnPoints[randomPos].position, resourceSpawnPoints[randomPos].rotation);
Destroy (gameObject);
}
My problem is that once the cube/harvester reaches the sphere it activates the sphere's destroy/instantiate script but then stops. It doesn't then move towards the newly instantiated sphere.
Any suggestions as to why this happens (or doesn't happen to be more precise)
???
Answer by Mmmpies · Feb 02, 2015 at 03:46 PM
All that's happening is you have the victim as a public transform and as soon as you destroy that game object it's gone. The new GameObject is something different.
Hold a reference to the first script in the second one, add a tag to the victim prefab. Also add a function in the the first script to search for gameObjects with that tag and set that as the new victim. Then, using the reference to first script call that new function after the new victim has been instantiated.
My JS is poor but I could probably put the code together if you're struggling.
I'm familiar with FindGameObjectWithTag("insertTag") tried a few variations of this but seemed to make things worse...lol
I'll have another try using your suggestions
I actually had a script that did pretty much what you suggested. I'd tries using this before but couldn't get it to work. Then realised that it relied on a start function (hence only got called once) As soon as I changed it to an Update function my follow object now happily moves around to whatever place the sphere spawns to.
private var targetPosition : Vector3;
private var directionNavAgent : Nav$$anonymous$$eshAgent;
function Update() //Change this back to a START function if it doesn't work
{
$$anonymous$$oveToClosest();
}
function $$anonymous$$oveToClosest()
{
directionNavAgent = GetComponent(Nav$$anonymous$$eshAgent);
FindClosestTarget();
directionNavAgent.destination = targetPosition;
}
function FindClosestTarget () : GameObject
{
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Resource");
var closest : GameObject;
var distance = $$anonymous$$athf.Infinity;
var position = transform.position;
for (var go : GameObject in gos)
{
var diff = (go.transform.position - position);
var curDistance = diff.sqr$$anonymous$$agnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
targetPosition = closest.transform.position;
return closest;
}
Something about your suggestion just triggered my to think about it.
$$anonymous$$any thanks :)