- Home /
cloned game object wont call script right...
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.
Perform Instantiate from a prefab, not an existing GameObject. Otherwise, have an extra fire which is hidden from view so you can always clone it.
$$anonymous$$aybe, disable the gameObject ins$$anonymous$$d of destroying it?
ive 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.
Drag the GameObject to the prefab and it should copy in the correct variables.
$$anonymous$$aybe back it up first :P
it 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.
Answer by Therian13 · Dec 09, 2013 at 07:34 AM
So I found a different way to do it. instead of just having two separate objects, I just made them one and enabled/disabled lights, effects, scripts, etc, at different times to make it APPEAR to be two different objects. Works just fine now, and is less irritating.
Thank you guys for your help earlier.
Answer by zerophase · Dec 06, 2013 at 09:54 AM
I'm guessing your object is being destroyed in the scene and you need to refind it.
if (GameObjectName == null)
GameObjectName = GameObject.Find("NameOfGameObject").GetCOmponent<ScriptName>();
Since it sounds like you're cloning the gameobject in the scene. you'll have to rename it when it's created in start.
gameObject.name = "GameObjectName";
I'm referring to the GameObject you are calling in your script. You'll have to place a call to Instantiate the GameObject after you destroy it, so you can add it back to script.
Instantiate works like this:
Instantiate(prefab, new Vector3(i * 2.0F, 0, 0),
Quaternion.identity)
The first parameter is the name of the prefab, the second is its location, and the third is its rotation.
Reataching the GameObject goes before you call that script for the first time, and I'd instantiate the GameObject around the same time you destroy it at.
im trying to reattach the game object, but it does not work. it keeps it at its current default setting (either my player prefab, not game player, or stays on none, even if I tell it to do it on awake or start.
Then you'll have to reassign those settings after reattaching.
How would I do that? as I said before, I am going in circles here.