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 Firedra · Mar 03, 2015 at 11:06 AM · c#classstructinstances

All Arrayinstances get changed

Hello, i am new here and i hope that somebody can help me with my problem. I am trying to create a script, which creates a mesh out of information from a textfile in a voxel design (the voxeleditor i use saves its models in a simple text format ^^)

For this task i did create my own Variabletype I called "Voxel", which simple saves a color as Vector3 and information about the shown faces/sides as a int-array (simply: is a side drawn (=1) or not drawn (=0)). Now i do have the problem that, when ever i set one face of one Voxelinstance to 1, all other instances of Voxel also get that value changed to 1. In the beginning i used "struct" and not "class" for the "Voxel"-Script, but i changed it just for checking if that was the fault. I hope i described everything cleary enough and that somebody has got a simple solution for me. Greetings, Firedra.

Voxel class:

    using UnityEngine;
     using System.Collections;
     [System.Serializable]
     public class Voxel{
         private Vector3 color;
         public int[] face;
         public Voxel (Vector3 c, int[] f)
         {
             color = new Vector3();
             face = new int[6];
             if (f.Length == 6){
                 color = c;
                 face = f;
             }
         }
         public void SetColor(Vector3 newColor)
         {
             color = newColor;
         }
         // Faceindendifier
         // 0 = Front
         // 1 = Back
         // 2 = Down
         // 3 = Up
         // 4 = Left
         // 5 = Right
         public void SetFace(int index)
         {
             this.face[index] = 1;
         }
         public void RemoveFace(int index)
         {
             this.face[index] = 0;
         }
         public Vector3 GetColor()
         {
             return color;
         }
         public int GetFace(int index)
         {
             return face[index];
         }
     }


Test Script to show the state of a face via GUI for 6 Voxelinstances.

 using UnityEngine;
 using System.Collections;
 
 public class _DebugVoxelFace : MonoBehaviour {
     public Voxel[] blocks;
     
     // Use this for initialization
     void Start () {
         blocks = new Voxel[6];
         int[] f = new int[6];
         f[0] = 0;
         f[1] = 0;
         f[2] = 0;
         f[3] = 0;
         f[4] = 0;
         f[5] = 0;
         blocks[0] = new Voxel(new Vector3(1,0,0),f);
         blocks[1] = new Voxel(new Vector3(2,0,0),f);
         blocks[2] = new Voxel(new Vector3(3,0,0),f);
         blocks[3] = new Voxel(new Vector3(4,0,0),f);
         blocks[4] = new Voxel(new Vector3(5,0,0),f);
         blocks[5] = new Voxel(new Vector3(6,0,0),f);
     }
     void OnGUI(){
         for(int i = 0; i < 6; i++){
             string s = blocks[i].GetFace(0).ToString();
             if (GUI.Button(new Rect(200+80*i,200,70,50),s)){
                 if (blocks[i].GetFace(0)==0) blocks[i].SetFace(0);
                 else blocks[i].RemoveFace(0);
             }
         }
     }
 }


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
Best Answer

Answer by NoseKills · Mar 03, 2015 at 11:09 AM

All your Voxel instances have a reference to the same array int[] f = new int[6]; that you create in _DebugVoxelFace.Start(). Whatever you put into that array is accessed by all Voxels.

You could just do

         blocks[0] = new Voxel(new Vector3(1,0,0), new int[6]);
         blocks[1] = new Voxel(new Vector3(2,0,0), new int[6]);
         blocks[2] = new Voxel(new Vector3(3,0,0), new int[6]);
         blocks[3] = new Voxel(new Vector3(4,0,0), new int[6]);
         blocks[4] = new Voxel(new Vector3(5,0,0), new int[6]);
         blocks[5] = new Voxel(new Vector3(6,0,0), new int[6]);

Comment
Add comment · Show 1 · 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 Firedra · Mar 03, 2015 at 09:52 PM 0
Share

Thank you very much! I allready knew it would be something little... ^^ But i didnt think that "f" would be such a trouble because i just copied the values of, atleast that is what i thought ^^. Have a nice day, sir!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Struct for holding Class Parameters 0 Answers

How -exactly- do classes and structs work in Unity? 0 Answers

Conversion of a static class to struct? 1 Answer

more Burgzerg Blues 1 Answer

Instancing class automatically? 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