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 Nicolas64pa · Aug 25, 2021 at 02:13 PM · boolinstatiate

Why does this function keep happening even when the bool assigned to make it happen is set to false?

So i have this code

 void RepeatGround()
     {
         
         if (distance > 10f && generateGround == true)
         {
             
             generateGround = false;
             Instantiate(ground, offset, Quaternion.identity);
           
         }
 
         offset = new Vector2(player.transform.position.x + 25f, -4.49f);
         if (distance < 2f)
         {
             generateGround = true;
         }
         
     }

It should first check if the distance is bigger than 10 and if generateGround is set to true, then if it is it should set generateGround to false and instatiate the gameobject once, instead it keeps instatiating it each frame even if the bool has been set to false, which i already checked and it does get set to false. Any idea on what do i have wrong? Or on how to do what i want in other ways?

Thank you in advance

Comment
Add comment · Show 2
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 AaronBacon · Aug 25, 2021 at 03:31 PM 1
Share

This all looks fine, so I'd have to assume the issue lies somewhere else in the script, like where generateGround is being defined, or maybe generateGround being set to true when it shouldn't be each frame. Are you able to paste the whole script?

⠀

My Other Recommendation would be add in a few Debug.Log(generateGround) before and inside the if statement, because according to what you're saying, they'll always return true since the if statement keeps returning true.

⠀

last thing to ask, is the script this is running on added to the object you instantiated, just to rule out any infinite recursion type thing.

avatar image Nicolas64pa AaronBacon · Aug 25, 2021 at 04:57 PM 0
Share
 public class GroundRepeater : MonoBehaviour
 {
 
     private Vector2 playerPos;
     private Vector2 groundPos;
     private Vector2 offset;
 
     private float distance;
     
     public GameObject player;
     public GameObject ground;
 
     private bool generateGround = false;
 
     // Start is called before the first frame update
     void Start()
     {
        
     }
 
     // Update is called once per frame
     void Update()
     {
         distance = Vector2.Distance(playerPos, groundPos);        
         groundPos = ground.transform.position;
         playerPos = player.transform.position;
         RepeatGround();
         Debug.Log(generateGround);
     }
     void RepeatGround()
     {
         
         if (distance > 10f && generateGround == true)
         {
             
             generateGround = false;
             Instantiate(ground, offset, Quaternion.identity);
           
         }
 
         offset = new Vector2(player.transform.position.x + 25f, -4.49f);
         if (distance < 2f)
         {
             generateGround = true;
         }
         
     }
 }


it is attached to the gameobject it instatiates

1 Reply

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

Answer by AaronBacon · Aug 25, 2021 at 05:25 PM

Looking at the full script, I believe I can see the problem. Its largely just to do with the order things are declared.

⠀

If all you want to know is the solution, you need to move the "distance = Vector2..." line in the Update function to be 2 lines lower, underneath where you set groundPos but above RepeatGround()

⠀

As for why it happens, when you begin the game, playerPos and groundPos haven't been given any value, so are taking the default value of (0,0). This means that when you run:

⠀

      void Update()
      {
          distance = Vector2.Distance(playerPos, groundPos);        
          groundPos = ground.transform.position;
          playerPos = player.transform.position;
          RepeatGround();
          Debug.Log(generateGround);
      }

⠀

The first time it runs, distance will return 0, (as you're setting distance first, before setting the other 2, and both groundPos and playerPos are still 0,0)

⠀

This means the first time RepeatGround() runs, distance is < 2f, so generateGround is set to true. Then the next frame, distance is set correctly this time, however the generateGround bool isn't set till after the Instantiate check, meaning that when it reaches the check for

⠀

"if (distance > 10f && generateGround == true)"

⠀

Both of these are true for the 2nd frame of the script running, so it instantiates an object. Normally everything is mostly set correctly from here on out, but the problem you're having is that this script instantiates another object with this script on. That new object has the same issue, since it begins with default values for playerPos and groundPos, so the cycle repeats, spawning a new object every other frame.

⠀

This means the solution to your issue is to move distance after playerPos and groundPos has been set, so that distance is calculated correctly the first time round:

⠀

      void Update()
      {   
          groundPos = ground.transform.position;
          playerPos = player.transform.position;
          distance = Vector2.Distance(playerPos, groundPos);
          RepeatGround();
          Debug.Log(generateGround);
      }

⠀

Hope that helps, if there's anything you don't get feel free to comment.

Comment
Add comment · Show 2 · 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 Nicolas64pa · Aug 25, 2021 at 05:39 PM 1
Share

This works perfectly, thank you very much and have a good day

avatar image AaronBacon Nicolas64pa · Aug 25, 2021 at 05:40 PM 0
Share

Thanks, and you're welcome =)

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

125 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 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 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 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 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 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 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

Update function is not executing the bool 1 Answer

store instantiated in Array. c# 1 Answer

Problem when setting variable in an instantiated gameobject 0 Answers

Convert bool to char in C# 3 Answers

Why is my variable always true? 1 Answer


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