Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 TangYC · Apr 10, 2016 at 03:55 PM · prefabresetvalue

The value is not reset for prefab.

Hi there.

I am facing a problems when I doing my game.

Its a defender game as the enemy hit your wall, your wall will be decreasing hp, till <=0, it destroyed. Everything is OK, the hp will reset to 10 when each play.

But after I made my wall into a prefab (it used to be just a object on scene), the Hp is not reset to the value I set (10) but continues from the previous play, and its not destroy itself anymore when Hp is <= 0...

Beginning of 1st time play, debug.log show hp = 10,

End of 1st time play, debug.log show hp = 5

Beginning of 2nd play, debug.log show hp = 5,

End of 2nd play, debug.log show hp = 0, (and its not destroying itself)

Beginning of 3rd play, debug.log show hp = 0,

End of 3rd play, debug.log show hp = -5,

This is my code for the Wall:

 using UnityEngine;
 using System.Collections;
 
 public class Wall : MonoBehaviour {
 
 
     public int castleHp = 10;
 
     // Use this for initialization
     void Start () 
     {
         castleHp = 10;
     }
 
     public void Damaging(int dmg)
     {
         Debug.Log (castleHp);
         castleHp = castleHp - dmg;
         Debug.Log (castleHp);
     }
         
 
     // Update is called once per frame
     void Update () 
     {
         if (castleHp <= 0)
             Destroy (gameObject);
     }
 }
 

And this is the code for one of the enemy:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAK : MonoBehaviour {
     
     public float lifeAK = 10;
     public float speed = 4.0f;
     bool shouldmove = true;
     public Wall wall;
     public int damage = 5;
 
 
     // Use this for initialization
     void Start ()
     {
         
     }
     
     void OnCollisionEnter (Collision col)
     {
         if (col.gameObject.tag == "CannonBall") 
         {
             lifeAK--;
         }
 
         if (col.gameObject.tag == "Walls") 
         {
             wall.Damaging (damage);
             shouldmove = false;
             Destroy (gameObject);
         }
     }
 
 
 
     // Update is called once per frame
     void FixedUpdate () 
     {
         //Destroy the object itself
         if (lifeAK == 0) 
         {
             Destroy (gameObject);
 
         }
 
         //walk/movement
         if(shouldmove == true)
         {
         transform.Translate(Vector3.left*Time.deltaTime*speed);
         }
     }
 }
 
Comment
Add comment · Show 4
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 Oribow · Apr 10, 2016 at 06:23 PM 0
Share

Are you reloading the scene between games? Or is it just a soft reset?

avatar image TangYC Oribow · Apr 11, 2016 at 07:07 AM 0
Share

I press the "Play" button in unity. Then press the "Pause" button, then the "Play" button again.

avatar image skylem TangYC · Apr 11, 2016 at 07:25 AM 0
Share

pausing the game wouldn't cause any changes in the scene so im a little confused as to your question.

Show more comments

2 Replies

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

Answer by KaushikRahul · Apr 11, 2016 at 01:27 PM

i Agree with skylem, if you make changes to the instantiated prefab it will directly affect the prefab as it works as a global object. Hence any changes made it it will remain.

If you want to work every time you must instantiate the prefab into another object and then try to change values, which wont affect the original prefab.

For example:

 GameObject objTemp = (GameObject)Instantiate(objToInstantiate);

Here any changes made to or the values of the "objTemp" won affect the original prefab "objToInstantiate".

Comment
Add comment · Show 1 · 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 Immanuel-Scholz · Apr 14, 2016 at 02:11 PM 0
Share

That is 100% correct, I just want to add that this has nothing to do with "prefabs". You can instantiate any object in Unity - be it self created or a prefab (which is really just a serialized version of an object).

avatar image
2

Answer by skylem · Apr 11, 2016 at 07:33 AM

When you instance a new object it uses the data from an object in your project folders, which is is universal whereever that prefab is used Example

if i wanted to instantiate a bullet and do something to the bullet once it was instanced you would instance using the Prefab as a reference but creating a new variable for this object to affect

 Transform spawnedBullet = (Transform)Instantiate(bullet, position, rotation);
  
 
 

spawnedBullet is now the object that is to be affected in future so that the prefab is always the same for new instances

Comment
Add comment · Show 4 · 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 TangYC · Apr 11, 2016 at 07:44 AM 0
Share

so thats mean i need to instantiate my castle?

avatar image TangYC · Apr 11, 2016 at 07:50 AM 0
Share

But before I made the wall into a prefab, It works well, able to reset the value for each pause and play...

avatar image skylem TangYC · Apr 11, 2016 at 02:54 PM 0
Share

is the reference on your script set to a gameobject in the scene or within your project folders ?

avatar image TangYC skylem · Apr 14, 2016 at 01:05 PM 0
Share

Within the project folders, yes.

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

7 People are following this question.

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

Related Questions

Inspector value resets on prefabs 1 Answer

Instantiate issues 1 Answer

How to reset a prefab used in object pool ? 2 Answers

Making a reset function for arrayed objects 2 Answers

Unity setting SerializedProperty.prefabOverride incorrectly 2 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