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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by hherzog · Jan 30, 2013 at 01:40 PM · gameobjectarrayclasseswallsblock

Strange behaviour after creating array of Blocks

I'm trying to create a wall of cubes and then shift their reference in the array and update their position, but when I tried to create a class to the cubes and inside of it a reference to a gameobject , everything goes wrong

 public class Block  
 {
     public GameObject obj;
    
     public Block()
     {
         
     }
     
 }

 Public class BlocksManager : MonoBehaviour
 {
     //global var
     Block[,] array;
     Object[] textures;  
     // Reference to another scrips
     public static BlocksManager instance;
     //the block it self
     Block block;
 
     void Awake()
     {
         instance = this;
         block = new Block();
     }

void Start()

    {
      // reference to the cubes
      //left space to create more after

        array = new Block[9, 9]; 
     //Load imagens of the especific patch
     textures = Resources.LoadAll("Textures");
   
     //create 8X8 Blocks
     for (int i = 0; i <= 7; i++)
     {
         for (int j = 0; j <= 7; j++)
         {
             array[i, j] = CreateCube(i, j);
             array[i, j].obj.name = "" + i + "," + j;
             
         }
     }

 Block CreateCube(int i, int j)
     {
 
         float referenceX = -2.071273f;
         float referenceY = -2.273989f;
         float cubeWidth = 0.60f;
         float cubeHeight = 0.60f;
    
         int i_ = i;
         int j_ = j;
 
         block.obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
         block.obj.transform.position = new Vector3(referenceX + j_ * cubeWidth, referenceY + i_ * cubeHeight, 0);
         block.obj.transform.localScale = new Vector3(0.60f, 0.60f, 0.60f);
         block.obj.renderer.material = new Material(Shader.Find("Transparent/Diffuse"));
         block.obj.renderer.material.mainTexture = textures[0] as Texture;
 
         return block;
 
     }

public void ChangeState(Collision colission) {

    //get the reference of the object
     //string[] array = colission.gameObject.name.Split(',');
    
     colission.gameObject.renderer.enabled = false;
     colission.gameObject.collider.enabled = false;
     MoveUpwards();
    
 }
 public void ChangeState(Collision colission)
     {
         colission.gameObject.renderer.enabled = false;
         colission.gameObject.collider.enabled = false;
         MoveUpwards();
     }


Here's the problem, when this is executed accours a problem with the blocks, when I used a array of GameObjects instead a array of Block everything works fine

public void MoveUpwards() {

     for (int i = 7; i >= 0; i--)
     {
         for (int j = 7; j >= 0; j--)
         {
             //move the blocks upwards
             float positionX = array[i, j].obj.transform.position.x;
             float positionY = array[i, j].obj.transform.position.y;
             print("I" + positionX);
             print("J" + positionY);

             array[i, j].obj.transform.position = new Vector2(positionX, positionY + 0.99f * 0.60f);
             //shift the reference
             array[i + 1, j] = array[i, j];
         }
     }

}

alt text

untitled-1.jpg (204.1 kB)
Comment
Add comment · Show 2
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 Wolfram · Jan 31, 2013 at 09:59 PM 2
Share

Ins$$anonymous$$d of just dumping 100 lines of code and waiting for somebody to read, understand, and reproduce in his heads what happens by pressing Play, how about you telling us what it's supposed to do, what do you expect to happen, and what do you actually want to happen ins$$anonymous$$d?

Explanations such as "$$anonymous$$ problem", "everything goes wrong", and "accours a problem" are not exactly helpful when describing an error...

avatar image hherzog · Jan 31, 2013 at 10:40 PM 0
Share

Sorry, but I don't know how explain well, so I posted the code, took one picture and explain some.

Here what happened when I press play, and what is happening:

When I hit space bar, the white cube destroy what is under it,but for a unknown reason the 8,8 block goes up. This happening when I changed the array of gameObjects for a array of type block.

When the block destroy something else, all the blocks going up and are destroyed when they reached the 9 line of blocks.

The first line of blocks are created when the 9 are destroyed.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Wolfram · Jan 31, 2013 at 10:56 PM

You only have a single object "block" in your whole manager. You initialize that in Awake(), but after that you just keep reassigning to block.obj of that one object. Note that CreateCube() doesn't actually create an object of type Block, it just returns a reference to your one BlockManager.block.

In the end, your whole array is filled with references to that same object, so there also is only one value block.obj, and it points to the last object you created in the last call to CreateCube, i.e. the object for CreateCube(7,7).

Solution: Move this line:

 block = new Block();

from Awake() to the beginning of CreateCube(). Also move the member variable block (line 20) into CreateCube as well.

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 hherzog · Feb 01, 2013 at 11:49 AM 0
Share

Strange the first time when I was using a array of gameobjects, I had done this way, but was creating besides the cubes some empty game objects too.

But now I remember, I had created a variable outside the method CreateCube, and only was initialized inside the method.

avatar image hherzog · Feb 01, 2013 at 12:06 PM 0
Share

Thaks you for the help but now strange things keeps happening.

When I hit spacebar to destroy a cube, they are destroy with some lag, and the variables positionX,positionY are accessed a lot of times ![alt text][1] [1]: /storage/temp/7392-untitled-2.jpg

When I hit twice or more, the wall of cubes doesn't move anymore. I don't know what's happening <_<

untitled-2.jpg (93.6 kB)
avatar image Wolfram · Feb 01, 2013 at 12:11 PM 2
Share

That's impossible to tell without seeing the code you execute when pressing Space (including any functions that are called).

avatar image
0

Answer by hherzog · Feb 01, 2013 at 08:59 PM

Here the code:

public class Esfarelante : MonoBehaviour {

     bool stepInsomething = false;
     Collision collision;
 
 
     // Update is called once per frame
     void Update()
     {
 
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             transform.Translate(new Vector3(-1, 0, 0) *     
             Time.deltaTime * 3.5f);
         }
 
         if (Input.GetKey(KeyCode.RightArrow))
         {
             transform.Translate(new Vector3(1, 0, 0) *  
             Time.deltaTime * 3.5f);
         }
 
         if (Input.GetKeyUp((KeyCode.LeftControl)))
         {
             if (stepInsomething)
             {
                
           BlocksManager.instance.ChangeState(this.collision);
                
             }
         }
     }
     void OnCollisionStay(Collision collision)
     {
         stepInsomething = true;
         this.collision = collision;
     }
 
     void OnCollisionExit(Collision collision)
     {
         stepInsomething = false;
     }
    
 }
 
Comment
Add comment · Show 9 · 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 Wolfram · Feb 01, 2013 at 09:12 PM 0
Share

This is not the script that handles pressing the spacebar.

Also, please post this new problem as a new question, as it is relatively unrelated to your original question, which has been answered and solved.

avatar image hherzog · Feb 01, 2013 at 09:45 PM 0
Share

I've changed the key to control key.

avatar image Wolfram · Feb 01, 2013 at 09:49 PM 0
Share

...thank you for mentioning that, now... -.-

avatar image Wolfram · Feb 01, 2013 at 09:53 PM 1
Share

When I hit spacebar to destroy a cube, they are destroy with some lag, and the variables positionX,positionY are accessed a lot of times

$$anonymous$$oveUpwards() contains a nested 8x8 loop, with 128 print statements in total. This is what makes it slow. Comment out both print statements, and it will run much faster.

avatar image Wolfram · Feb 01, 2013 at 11:49 PM 1
Share

Too many possible factors, too many possibilities. Also, we don't know how you did fix the reference problem, which could have other side effects.

If the GameOBject approach worked for you, I'd suggest you use it, and ins$$anonymous$$d of having the standalone Block-classes referencing your object, have Block derive from $$anonymous$$onoBehaviour, and add it as a regular component to your GameObject, as soon as you instanced that (=directly after your GameObject.CreatePrimitive line).

Show more comments

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

10 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

Related Questions

I see a sky through the building 1 Answer

Instantiate from array into array? 2 Answers

how to check for GameObject is null in array with random 1 Answer

Use an objects (from array) position to focus a camera on 3 Answers

Cannot convert 'UnityEngine.Collider[]' to 'UnityEngine.gameObject[]' using OverlapSphere 3 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