• 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 StuwuStudio · Mar 30, 2015 at 09:11 AM · gameobjecttransformvariablesskyboxes

Generate Multiple Object and Mesure the Distance

Hi, i've got a problem concerning for calculate the distance of multiple instantiates items

Firstly, i need to generate multiple item;

  for(int i = 1; i <= planetsNumber; i++) {
                 floatName = floatName + 1f;
                 Vector3 position01 = new Vector3(Random.Range(-1000.0f, 1000.0f), Random.Range(-1000.0f, 1000.0f), Random.Range(-1000.0f, 1000.0f));
                 newPlanets = (GameObject)Instantiate(planet01, position01, Quaternion.identity); //Assing the skybox script to the current planet create a curent planet "foreach" planets
                 planetsName = System.Convert.ToString(floatName);
                 newPlanets.name = "planets" + planetsName;
                 newOzone = (GameObject)Instantiate(Ozone, position01, Quaternion.identity);
                 newOzone.name = "ozone" + planetsName;
             }


Next step, i need to calculate distance of multiple object;

 Allpl = GameObject.FindWithTag("Planets01").transform;
         foreach(Transform Allpl in Allpls.transform) {
             float DistPlanets = Vector3.Distance(Allpl.position, Player.position);
             if(DistPlanets <= 40) {
                 RenderSettings.skybox = skybox2;
                 RenderSettings.fog = true;
             }
             if(DistPlanets >= 40) {
                 RenderSettings.skybox = skybox1;
                 RenderSettings.fog = false;
             }

My error, is not able to find the "Object" with the Planets01 tag and when i approach one of the planet, this planets do the near action but the others planets do the far action

near:

 if(DistPlanets >= 40) {
                 RenderSettings.skybox = skybox1;
                 RenderSettings.fog = false;
             }


far:

 if(DistPlanets <= 40) {
                 RenderSettings.skybox = skybox2;
                 RenderSettings.fog = true;
             }

The complet script:

using UnityEngine; using System.Collections;

public class PlanetsRandomGenerating : MonoBehaviour {

 public GameObject planet01;
 public GameObject Ozone;
 public float planetsNumber;
 GameObject Allpls;
 GameObject newPlanets;
 GameObject newOzone;
 Transform Player;
 string planetsName;
 float floatName;

 public Material skybox1 = null;
 public Material skybox2 = null;
 
 // Use this for initialization
 void Start () {
     for(int i = 1; i <= planetsNumber; i++) {
         floatName = floatName + 1f;
         Vector3 position01 = new Vector3(Random.Range(-1000.0f, 1000.0f), Random.Range(-1000.0f, 1000.0f), Random.Range(-1000.0f, 1000.0f));
         newPlanets = (GameObject)Instantiate(planet01, position01, Quaternion.identity); //Assing the skybox script to the current planet create a curent planet "foreach" planets
         planetsName = System.Convert.ToString(floatName);
         newPlanets.name = "planets" + planetsName;
         newOzone = (GameObject)Instantiate(Ozone, position01, Quaternion.identity);
         newOzone.name = "ozone" + planetsName;
     }
 }
 
 // Update is called once per frame
 void Update () {
     Allpl = GameObject.FindWithTag("Planets01").transform;
     foreach(Transform Allpl in Allpls.transform) {
         float DistPlanets = Vector3.Distance(Allpl.position, Player.position);
         if(DistPlanets <= 40) {
             RenderSettings.skybox = skybox2;
             RenderSettings.fog = true;
         }
         if(DistPlanets >= 40) {
             RenderSettings.skybox = skybox1;
             RenderSettings.fog = false;
         }
     }
 }

}

Another problem:

error CS0136: A local variable named Allpl' cannot be declared in this scope because it would give a different meaning to Allpl', which is already used in a `parent' scope to denote something else

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 NoseKills · Mar 30, 2015 at 04:01 PM 0
Share

The error comes from this

 Allpl = GameObject.FindWithTag("Planets01").transform;
 foreach(Transform Allpl in Allpls.transform) 
 {...

You apparently have a field called Allpl declared in the class but you try to use a variable with the same name as the loop var in the foreach too. You must rename either of the variables.

you don't actually say what you want your code to do. You want to change the skybox when close enough to a planet ? Then you have to break out of the foreach when you find a planet that's close enough. Now you'll loop through all planets and if the las ät one happens to be far away, the "far skybox" will be the last one you set.

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to assign x and y transform values of an gameobject to user defined variables? 1 Answer

GameObject Rotation is always postive 0 Answers

How to make an array of GameObjects that stores their transforms? 1 Answer

Instantiate rigidbody, transform or gameobject? 1 Answer

I want to move a cube with rotation but I find this problem 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