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 /
avatar image
0
Question by Smasham · Jun 11, 2019 at 01:32 AM · mesh3dcubemeshfilterperlin noise

Game Object's Vector3 changing threw script without reason

0

I am trying to do a combine block script. However, i encountered 2 problems: the blocks / the combined mesh isn't showing up anymore, and when it was, all the blocks where at the same location (even tho the blocks are not showing up, i still ask to display the locations in the Debug Log and they are all the same). I check the locations at multiple places in the script, and i don't see why they changed.

  using System.Collections; 
 using System.Collections.Generic; 
 using System.Linq;
  using UnityEngine;
  public class ThisWorldGen : MonoBehaviour { 
 public GameObject meshy;
  public GameObject blockPrefab; 
 public GameObject grass;
  public GameObject dirt; 
 public GameObject rock;
  private string[] blockTag;
  private GameObject stockin;
 
 public int amp;
 public int freq;
 private Vector3 mypos;
 [SerializeField]
 private Material material;
 private Vector3 blockLocations;
 
 public List<MeshFilter> meshes = new List<MeshFilter>();
 
 void Start()
 {
     Generate();
 }
 
 void Generate()
 {
     amp = 2;//Random.Range(0,100);
     freq = 2;//Random.Range(30,100);
     mypos = this.transform.position;
     int cols = 100;
     int rows = 100;
     float startTime = Time.realtimeSinceStartup;    
     #region Create Mesh Data
     MeshFilter blockMeshes = Instantiate(blockPrefab, Vector3.zero, Quaternion.identity).GetComponent<MeshFilter>();
 
     for(int x = 0; x < cols;x++)
     {
         for(int z = 0; z < rows;z++)
         { 
             float y = Mathf.PerlinNoise ((mypos.x + x) / freq,(mypos.z + z)/freq) * amp;
             y = Mathf.Floor(y);
             int a1 = 0;
             for(float hy = y; hy > 0.0F; hy--)
             {           
 
                 blockMeshes.transform.position = new Vector3(mypos.x + 
  x,y,mypos.z +z);
                 Debug.Log(blockMeshes.gameObject.transform.position);//the 
  blocks positions are normal (all different)
                 meshes.Add(blockMeshes);  
                 Debug.Log(meshes[a1].gameObject.transform.position);//the 
  blocks positions are normal (all different)
             }
         }
     }
     int i = 0;
     MeshFilter[] listMeshes = new MeshFilter[meshes.Count];
 
     while(i < (meshes.Count))
     {   
 
         listMeshes[i] = meshes[i];
         Debug.Log(meshes[i].gameObject.transform.position);//the blocks 
  position are all the same (99.0,1.0,89.0)
         Debug.Log(listMeshes[i].gameObject.transform.position);//the blocks 
  position are all the same (99.0,1.0,89.0)
         i++;
     }
 
 
     CombineInstance[] combine = new CombineInstance[listMeshes.Length];
     int w = 0;
     while(w < listMeshes.Length)
     {   
 
         Debug.Log(listMeshes[w].gameObject.transform.position);//the blocks 
  position are all the same (99.0,1.0,89.0)
 
 
 
 
         combine[w].mesh = listMeshes[w].sharedMesh;
 
         combine[w].transform = listMeshes[w].transform.localToWorldMatrix;
         Destroy(listMeshes[w].gameObject);
         w++;
     }
 
     Mesh finalMesh = new Mesh();
     finalMesh.CombineMeshes(combine);
     meshy.GetComponent<MeshFilter>().sharedMesh = finalMesh;
     meshy.SetActive(true);
     #endregion
     Debug.Log("Loaded in " + (Time.realtimeSinceStartup - startTime) + " Seconds.");
 
 }
 }

so my two questions are: How do i get to display the cubes, and why are they all placed at 99.0,1.0,89.0 for no reason ?

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 Bunny83 · Jun 11, 2019 at 01:50 AM

You create one single object here:

 MeshFilter blockMeshes = Instantiate(blockPrefab, Vector3.zero, Quaternion.identity).GetComponent<MeshFilter>();

which you add several times into your meshes list here:

 meshes.Add(blockMeshes);

So you end up with a list that contains several references to the exact same object.

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 Smasham · Jun 11, 2019 at 02:12 AM 0
Share

then, why when i ask for all the locations at line 51 and 54, i get different locations ?

avatar image Smasham Smasham · Jun 11, 2019 at 02:16 AM 0
Share

ok apparently, it seems to work

avatar image Bunny83 Smasham · Jun 12, 2019 at 12:13 AM 0
Share

Because you check / log the position inside the loop right after you changed the position of that single gameobject. The actual position that your gameobject will have once your loop is completed is the last set position.

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

142 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

Related Questions

How do i replace a Mesh Filter?? 1 Answer

how to render 60k cubes as efficiently as possible? 1 Answer

How do i modify a mesh Filter(C#)? 2 Answers

Unity5 Procedural meshing slower than in Unity4? 1 Answer

Mirror vertices procedurally 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