Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Therian13 · Dec 06, 2013 at 07:17 AM · prefab

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Therian13 · Dec 06, 2013 at 07:00 AM 0
Share

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.

avatar image meat5000 ♦ · Dec 07, 2013 at 10:22 PM 0
Share

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?

avatar image Therian13 · Dec 08, 2013 at 04:20 AM 0
Share

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.

avatar image meat5000 ♦ · Dec 08, 2013 at 09:41 AM 0
Share

Drag the GameObject to the prefab and it should copy in the correct variables.

$$anonymous$$aybe back it up first :P

avatar image Therian13 · Dec 08, 2013 at 01:47 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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";
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zerophase · Dec 07, 2013 at 09:28 AM 0
Share

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.

avatar image zerophase · Dec 07, 2013 at 10:20 AM 0
Share

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.

avatar image Therian13 · Dec 07, 2013 at 10:37 AM 0
Share

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.

avatar image zerophase · Dec 07, 2013 at 11:08 AM 0
Share

Then you'll have to reassign those settings after reattaching.

avatar image Therian13 · Dec 07, 2013 at 11:13 AM 0
Share

How would I do that? as I said before, I am going in circles here.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Selecting and deselecting not working 0 Answers

Instantiate problem prefab wrong position in child object.... 0 Answers

how to stack prefabs (with script) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges