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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by asduffo · Jul 30, 2013 at 04:23 PM · c#spawnincrementdecrease

Strange error with increment-decrement between scripts

*bad engrish incoming"

Hi everyone

Basycally i'm creating a game where you have to drag into the scene some objects to do things.You also have an "energy bar". Everytime you spawn an object, the energy decreases. If it reaches zero, you can't spawn anymore object; however if you drag one of the dragged object into a "garbage icon" it increases again.

So i created a "generator" of the object to drag: the portion of the script that spawns the object is that:

 public Transform clone; //a clone of block
 public Transform block; //the block that i need to be renderized
     void OnMouseDown()
         {
             print("touched!");
             if (energy.GetComponent<Energyscript>().avaiable >= required)
             {
                 clone = (Transform)Instantiate(block, transform.position, Quaternion.identity);
                 energy.GetComponent<Energyscript>().avaiable -= required; //required is the amount of energy required from the object to spawn
                 print(energy.GetComponent<Energyscript>().avaiable);
             }
             else //if the energy is not enough
             {
                 //beep!
                 print("beep");
             }
         }
 

the energy script is basically that:

public class Energyscript: MonoBehaviour { public int avaiable; }

Easy. you set directly into the scene the amount of energy you want.

However that's the script inside the object to be spawned after touching the generator (without some parts that surely aren't the problem)

 void OnMouseUp()
     {
         //if we dragged the object into the garbage icon
         if (nullArea)
         {
             if (slot != null) //nevermind about slot. look immediately above
             {
                 slot.GetComponent<Slot>().occupato = false;
             }
             //look at here and all of the similiar parts:
             energy.GetComponent<Energyscript>().avaiable+= required;
             print(energy.GetComponent<Energyscript>().avaiable);
             Destroy(gameObject);
         }
         if (!play.gameObject.GetComponent<playControl>().toccato)
         {
             toccato = false;
             slot = cercaVicino();
             if (slot != null && !nullArea)
             {
                 if (!slot.GetComponent<Slot>().occupato)
                 {
                     transform.position = new Vector3(slot.transform.position.x, slot.position.y, 0.0F);
                     slot.GetComponent<Slot>().occupato = true;
                 }
                 else
                 {
                     transform.position = latest;
                     slot = cercaVicino();
                     slot.GetComponent<Slot>().occupato = true;
                 }
             }
         }
     }
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0) && creato)
         {
             toccato = true;
         }
         if(Input.GetMouseButtonUp(0) && creato)
         {
             slot = cercaVicino();
             if (areaNulla)
             {
                 if (slot != null)
                 {
                     slot.GetComponent<Slot>().occupato = false;
                 }
                 energy.GetComponent<Energyscript>().avaiable += required;
                 print(energy.GetComponent<Energyscript>().avaiable);
                 Destroy(gameObject);
             }
             else
             {
                 if (slot != null)
                 {
                     if (slot.GetComponent<Slot>().occupato)
                     {
                         energy.GetComponent<Energyscript>().avaiable+= required;
                         print(energia.GetComponent<Energia>().disponibile);
                         Destroy(gameObject);
                         
                     }
                     else
                     {
                         transform.position = new Vector3(slot.transform.position.x, slot.position.y, 0.0F);
                         slot.GetComponent<Slot>().occupato = true;
                     }
                 }
                 creato = false;
                 toccato = false;
             }
         }
     }

Note that the block that must be spawned is in the resources folder (not in the scene). The generator and energy are both in the scene.

However: i run the game: everything is normal; energy is set at 10, same for required (in both of generator and the object to spawn)

i touch the generator, the object spawns and i drag the object into the scene; the console says to me that now energy is at 0, as expected; BUT THEN:

i drag the spawned block into the garbage icon: it gets destroyed and the log says to me that now energy is at 0; BUT if i click again the generator (and it's supposed to spawn another object since energy is again at 10), it just logs to me a "beep", just like i don't have anymore energy. In fact, if i watch the energy object in the inspector, it says to me that energy is at 0.

But now the 2nd weirdest thing: if i restart the scene (i have a button that if clicked it runs the code Application.LoadLevel(Application.loadedLevelName);), and drag again the object into the scene and then i drag the object into the garbage, it logs to me 20! And still can't spawn new object (and energy is still at 0); The same thing happens if i directly drag the object into the garbage immediately after it spawns.

The 3rd weirdest thing is that it continues to increase starting with the latest saved value (in that case 20) even if i actually kill the game (by pressing the play button) and restarting again, and it resets only if i close unity and open again it.

Since i really can't find a solution, could someone help me?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Xtro · Jul 30, 2013 at 05:50 PM

Aren't you supposed to decrease the enery in the first script ?

energy.GetComponent().avaiable -= required;

instead of...

energy.GetComponent().avaiable += required;

Comment
Add comment · Show 3 · 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 asduffo · Jul 31, 2013 at 12:59 PM 0
Share

i really dunno how it happened but actually the line in my script had -=, not += (i edited the question)

However it doesn't solve my problem.

avatar image Xtro · Jul 31, 2013 at 01:26 PM 0
Share

It's really hard to figure it out without seeing your setup. Can you create a smaller project(only adding these parts) and attach here ?

avatar image asduffo · Aug 01, 2013 at 03:22 PM 1
Share

never$$anonymous$$g. i found the solution by myself. Basically the error occurred because i used as energy a prefab in the resource folder and not the actual gameobject in the hierarchy. I used ins$$anonymous$$d gameobject.find and now it works well. $$anonymous$$any thanks for your time spent trying to help anyway :D

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

16 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Text Prefrab problem 1 Answer

Generate gameObject horde, more positioned towards front of horde 1 Answer

Make enemy spawn only if the previous one spawned it out of the way. 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