I have problem when making a prefab.
i have an enemy targeting a chest in my game it works in the hirearchy but when i drag it to make it a prefab it deletes the target(transform) can somebody help me!!
Sorry for the bad post layout
I have problem when making a prefab.
i have an enemy targeting a chest in my game it works in the hirearchy but when i drag it to make it a prefab it deletes the target(transform) can somebody help me!!
Sorry for the bad post layout
You can’t use a transform in the hierarchy for a variable on a prefab. It will ALWAYS delete it. You need to edit your script to find the transform when the game is running.
Thanks for replying,
Well this is the code i have for it, how would i add to search for the transform in the code itself. thank you
var target: Transform;
var startTime;
var timer1: int;
var timingOn: boolean;
var GetReadyButton: boolean;
var font : Font;
function Update()
{
if(Input.GetButton("StartWave"))
{
timingOn = true;
startTime = Time.time; //time starter
}
if (timingOn)
{
TimerStart();
}
}
function OnGUI ()
{
GUI.skin.label.font = font;
GUI.Label (Rect (400, 40, 100, 20),"Press G To Start"); //First Text to tell player to press g to start
if(Input.GetButton("StartWave"))
{
GetReadyButton = true;
}
if(GetReadyButton)
{
GUI.Label (Rect (415, 55, 100, 20),"10 Seconds");
}
}
function TimerStart()
{
timer1 = Time.time; //Set time
if(timer1 - startTime >= 10)
{
Debug.Log ("Timer finished, spawn now");
GetComponent(NavMeshAgent).destination = target.position;
target = GameObject.FindWithTag("Chest").Transform;
}
}
It’s already there on line 47. Just make sure that the target is actually tagged “Chest” and you should be fine.
Unfortunately, it is still not moving towards the chest, i have made a spawner and it spawns the GoblinRef prefab but stays still
Well, you are setting the destination before setting the target so maybe that is your problem. Swap lines 46 and 47 and see if that makes any difference.
If i swap that around the ones in the hierarchy dont move now and the spawner ones still dont move, but i i keep the cod how it was the ones in the hierarchy do move towards the target just not from the spawner. if that makes sense
When in game i apply the chest to it and it works its just doesn’t automatically search for it when first spawned
The only other suggestion I have is to try to move line 47 in the Update like this:
if(!target)
{
target = GameObject.FindWithTag("Chest").Transform;
return;
}
Make it the first thing in the update, that way nothing else in the script happens if there is no target.
If that doesn’t work, you should probably take your script to the scripting forum and ask for help there. I just don’t know enough Unity Script to help you any further.