The question is answered, right answer was accepted
Create new transform when other transfrm is destroyes
Hi,
I'm making an AI script for a enemy, it needs to wander end set its own destination inside of a given area. Now I made this script by following a couple tutorials, but when I destroy the moveSpot.gameobject and want to create a new transform i get the error that its still trying to acces it.
This is my update function:
{ //CreateMoveSpot(); moveSpot.localPosition = center + new Vector3(UnityEngine.Random.Range(-size.x / 2, size.x / 2), 0, (UnityEngine.Random.Range(-size.z / 2, size.z / 2))); //Vector3 pos = center + new Vector3(Random.Range(-size.x / 2, size.x / 2), 1,(Random.Range(-size.z / 2, size.z / 2))); moveSpot = Instantiate(moveSpot, moveSpot.localPosition, Quaternion.identity) as Transform; moveSpot.name = "MoveSpot"; } else if (moveSpot != null) { agent.SetDestination(moveSpot.position); isWandering = true; } if (Vector3.Distance(transform.position, moveSpot.position) < 1f) { Destroy(moveSpot.gameObject);if (moveSpot.transform == null)
} I'm a beginner so the code looks a bit weird maybe. Hope someone can help me!
Answer by Kasper2000 · Apr 02, 2020 at 11:30 AM
I was thinking in the wrond way, instead of creating a new movespot I could just change the position(ofcourse .....);
this is the code now
if (moveSpot != null) { agent.SetDestination(moveSpot.position); if (Vector3.Distance(transform.position, moveSpot.position) < 1f) { moveSpot.localPosition = center + new Vector3(UnityEngine.Random.Range(-size.x / 2, size.x / 2), 0, UnityEngine.Random.Range(-size.z / 2, size.z / 2)); Debug.Log(moveSpot.position); } }
Follow this Question
Related Questions
Help me destroy anplayer when out of bounds. 2 Answers
Objects getting destroyed in unity test, but only sometimes in the phone app 0 Answers
GameObject not destroying on Collision 1 Answer
Unity - Opposite of instantiate - destroyed but not removed 0 Answers
2D sprite won't destroy on collision 0 Answers