• 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
Question by AndrewQuinn737 · Jul 14, 2022 at 12:01 PM · floatspawningspawning problemsaddingfloats

Floats not being added to other floats?

Hi

I'm trying to build a script that generates hexagons with the click of a button. But whenever they spawn all of them are in the wrong x position because a float isn't being added in my script. I included the relevant part of my script.

The problem is with the lines that say "xPosition = xBase * 1732051 / 1000000 + 1732051 / 1000000 / 2;"

For some reason it will multiply 1732051 / 1000000 to the x base but not add the second part. I tried making a public variable/float that was equal to that number and adding that and I also tested multiplying it but for some reason unity registers it as a zero.

I really don't know what to do here none of the other answers really covered this particular issue. Please help

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Spawn : MonoBehaviour
 {
     //Hexagons: water, dirt, rock
     public GameObject Water;
     public GameObject Dirt;
     public GameObject Rock;
     public GameObject spawnButton;
     public float xPosition;
     public float yPosition;
     public float zPosition;
     public float multNumber = 1732051 / 1000000 / 2;
     public float position;
     public float positionNumber;
     public float xBase;
     public float xBase2;
     public float zBase;
     public float sphereRadius;
     public float maxDistance;
     public LayerMask layerMask;
 
     private Vector3 objectPosition;
     private Vector3 direction;
 
     void Start()
     {
 
     }
 
     void Update()
     {
 
     }
 
     public void startSpawn()
     {
         //functions the button calls on click
         StartCoroutine(SpawnObjects());
         StopCoroutine(SpawnObjects());
         Destroy(spawnButton);
     }
 
     Vector3 pickPosition()
     {
         positionNumber = Mathf.Round(Random.Range(0, 7));
 
         //each of these (0-6) generates in a different z range bc the hexagons have to be placed at different x values to fit together
 
         if (positionNumber == 0)
         {
             xBase = Mathf.Round(Random.Range(-5, 5));
             yPosition = Mathf.Round(Random.Range(50, 50));
             zPosition = Mathf.Round(Random.Range(0, 0));
             xPosition = xBase * 1732051 / 1000000;
         }
 
         if (positionNumber == 1)
         {
             xBase = Mathf.Round(Random.Range(-5, 5));
             Debug.Log(xBase);
             yPosition = Mathf.Round(Random.Range(50, 50));
             zBase = Mathf.Round(Random.Range(1, 1));
             zPosition = zBase * 3 / 2;
             xPosition = xBase * 1732051 / 1000000 + 1732051 / 1000000 / 2;
             Debug.Log(xPosition);
         }
 
         if (positionNumber == 2)
         {
             xBase = Mathf.Round(Random.Range(-5, 5));
             yPosition = Mathf.Round(Random.Range(50, 50));
             zBase = Mathf.Round(Random.Range(0, 3));
             zPosition = zBase * 3;
             xPosition = xBase * 1732051 / 1000000;
         }
 
         if (positionNumber == 3)
         {
             xBase = Mathf.Round(Random.Range(-5, 5));
             Debug.Log(xBase);
             yPosition = Mathf.Round(Random.Range(50, 50));
             zBase = Mathf.Round(Random.Range(0, 3));
             zPosition = (zBase * 2 - 1) * 3 / 2;
             xPosition = xBase * 1732051 / 1000000 + 1732051 / 1000000 / 2;
             Debug.Log(xPosition);
         }
 
         //negatives
 
         if (positionNumber == 4)
         {
             xBase = Mathf.Round(Random.Range(-5, 5));
             Debug.Log(xBase);
             yPosition = Mathf.Round(Random.Range(50, 50));
             zBase = Mathf.Round(Random.Range(-1, -1));
             zPosition = zBase * 3 / 2;
             xPosition = xBase * 1732051 / 1000000 + 1732051 / 1000000 / 2;
             Debug.Log(xPosition);
         }
 
         if (positionNumber == 5)
         {
             xBase = Mathf.Round(Random.Range(-5, 5));
             yPosition = Mathf.Round(Random.Range(50, 50));
             zBase = Mathf.Round(Random.Range(-3, 0));
             zPosition = zBase * 3;
             xPosition = xBase * 1732051 / 1000000;
         }
 
         if (positionNumber == 6)
         {
             xBase = Mathf.Round(Random.Range(-5, 5));
             Debug.Log(xBase);
             yPosition = Mathf.Round(Random.Range(50, 50));
             zBase = Mathf.Round(Random.Range(-3, 0));
             zPosition = (zBase * 2 + 1) * 3 / 2;
             xPosition = xBase * 1732051 / 1000000 + 1732051 / 1000000 / 2;
             Debug.Log(xPosition);
         }
 
         Vector3 objectPosition = new Vector3(xPosition, yPosition, zPosition);
         RaycastHit hit;
         direction = transform.forward;
         sphereRadius = 1/8;
         maxDistance = 0;
 
         //this below is what is supposed to stop hexagons from spawning same place
 
         if (Physics.SphereCast(objectPosition, sphereRadius, direction, out hit, maxDistance, layerMask, QueryTriggerInteraction.UseGlobal))
         {
             pickPosition();
         }
 
         return objectPosition;
         }
     }
 }
Comment

People who like this

0 Show 0
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

Answer by AndrewQuinn737 · Jul 15, 2022 at 11:56 PM

Actually I figured out that I just had to declare the value of the variable being added in the unity editor and then it started registering and it works now

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

188 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

How to spawn multiple prefabs at different spawn rates 0 Answers

Can you do float + float 1 Answer

How to rotate an object 2 Answers

Spawn Object on Set Score 0 Answers

Items spawn and will not stop spawning. 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