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 /
  • Help Room /
avatar image
0
Question by Nis5l · Nov 26, 2018 at 10:25 PM · floatforloop

float in for loop isnt changing

Hello, I tried creating a RTS prototype but im stuck with the formation of the units.A float (posadd) is after the loop changing back to 0 but in the forloop the float somehow starts again with the number of the forloop i did before(Console). Because of that the Units move even further down on every click...

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class UnitMovement : MonoBehaviour { public List<GameObject> units = new List<GameObject>(); Unit[] oldunits; bool clicked; public static RaycastHit hit; public static Vector3[] positions; private float posadd; void Start() { } void Update() { if (Input.GetMouseButtonDown(1)) //if mouse is clicked set bool to true { clicked = true; } if (clicked) { Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit); //ray to point i clicked oldunits = FindObjectsOfType<Unit>(); //get all units foreach (Unit uni in oldunits) //search if units are selected { if (uni.select == true) { units.Add(uni.gameObject); //put selected in list } } for (int i = 0; i < units.Count; ++i) //loop threw them { posadd += 2; //add 2 to distance units[i].GetComponent<Unit>().moveposition = new Vector3(hit.point.x + posadd, hit.point.y, hit.point.z); //add the distance to the position units[i].GetComponent<Unit>().move(); //give move command to unit (Navmesh) Debug.Log(posadd); } posadd = 0; //make distance 0 Debug.Log(posadd); clicked = false; //set clicked to false } } } it would be very nice if someone could help me. first question here btw, but i hope i described my problem good egnoth...

alt text alt text

unbenannt.png (35.0 kB)
unbenannt.png (251.7 kB)
Comment
Add comment · Show 1
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 Nis5l · Nov 26, 2018 at 10:29 PM 0
Share

Im soory but I dont know why the code got pushed together like this in the preview it was normally!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Heck134 · Nov 27, 2018 at 07:29 PM

Hi there @kirchler123,


This is an extremely strange issue and I can't see a cause myself; but as you currently have posadd as a global variable, you could try declaring it right before your forloop so that it is a new variable each time you hit the loop. That should guarantee that no value can be carried over. This is more of a Band-Aid fix, (and doesn't explain whats going on) but if you won't need to use posadd anywhere outside that loop it should be fine.


Relevant Changes;

 bool clicked; 
 public static RaycastHit hit; 
 public static Vector3[] positions; 
 private float posadd; <------------------- Remove Me
       
 
     if (clicked) {
         DoTheUnitsStuff()
         private float posadd;
         for (int i = 0; i < units.Count; ++i) {
             DoTheMovement()
         } 
         posadd = 0; <------------------- Remove Me
     }


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

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

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

SetSpeed float C# 1 Answer

Mathf.Round ? [C#] 2 Answers

Car is Flying instead of driving 1 Answer

subtracting from float in a user made function 0 Answers

I need to synchronize 2 float values ​​of each character to another 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