Hello,
I am currently making a survival game, and am currently having trouble with one of my scripts. when the game starts, I have 2 camp fires, one is lit, and the other one is not. if the first one dies (gets destroyed) , I wont be able to light the unlit one (which just replaces it with the lit model). it gives an error stating"The object of type ‘Transform’ has been destroyed but you are still trying to access it".
So, I know it is trying to call the previously done model, but since it was destroyed, it cant call it.
So I tried having it call a prefab of the lit object. it spawns it in right, but the now lit campfire wont read the script right. it wont let me assign my player in the scene to the script, so I thought I could just make a prefab of the player and try that.
it spawns in fine, and doesnt give any errors, but it does not heal my character like it should. im assuming it is trying to heal the prefab, and not my character…
could anyone tell me how to fix this please? I appreciate it. below are my two scripts for the camp fire.
The first is for the Unlit campfire:
#pragma strict
var thefirepit : Transform;
private var drawFireGUI = false;
private var FireIsUnlit = true;
function Update ()
{
if (drawFireGUI == true && Input.GetKeyDown(KeyCode.E))
{
StartFireState();
}
}
function OnTriggerEnter (theCollider :Collider)
{
if (theCollider.tag == "Player")
{
drawFireGUI = true;
}
}
function OnTriggerExit (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
drawFireGUI = false;
}
}
function OnGUI ()
{
if (drawFireGUI == true)
{
GUI.Box (Rect (Screen.width*0.5-51, 250, 180, 26), "Press E to Light Fire");
}
}
function StartFireState ()
{
var cloneFirePit : Transform;
cloneFirePit = Instantiate(thefirepit,transform.position,transform.rotation);
Destroy(this.gameObject);
}
`
The second script is for the Lit campfire :
var Distance;
var Target : Transform;
var BarelyWarmTemp = 2.0;
var GoodWarmTemp = 1.0;
var TooHotRange = 0.4;
var Heat : PlayerStatsV2;
function Start ()
{
Destroy(this.gameObject,5);
}
function Update ()
{
{
Distance = Vector3.Distance(Target.position, transform.position);
}
if (Distance < BarelyWarmTemp)
{
Heat.SlightlyWarm ();
}
if (Distance < GoodWarmTemp)
{
Heat.NiceWarm ();
}
if (Distance < TooHotRange)
{
Heat.TooCloseToFire ();
}
}
Thank you for your help on this. I think I am close to getting it right, but just cant quite figure it out.
I apologize for the scripts not being in the box properly. it seems to be having trouble with it, no matter how much I edit it.
– Therian13Perform Instantiate from a prefab, not an existing GameObject. Otherwise, have an extra fire which is hidden from view so you can always clone it. Maybe, disable the gameObject instead of destroying it?
– meat5000ive been trying to disable the game object, but was having issues with it since I use scripts on it. it always gives me some random error (like I need to add a ";" although I already have one) As for instantiate a prefab, I can do this, but it wont assign my player in the scene to the variables Target and Heat. It keeps trying to do it to the ones in the prefab. so my character wont warm up near the fire or take damage if he is on it.
– Therian13Drag the GameObject to the prefab and it should copy in the correct variables. Maybe back it up first :P
– meat5000it doesn't work. I cant save my on scene character to the Prefab's script. it wont let me. and if I make a prefab of the character and reimport that into the scene, it still wont affect the on scene character.
– Therian13