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 ArunChnadran · Mar 12, 2012 at 12:21 PM · transformpositionx-axis

got problem with x's position!

Trying to get the position of an Instance from one code. Here goes my first code:-

 var redCube: GameObject;
 var blueCube: GameObject;
 var currentCube: GameObject;
 
 var thegrid = new int[7,5];
 
 var cubeSwitch: boolean = false;
 var flag:boolean = false;
 currentCube=redCube;
 
 function Start()
 {
 
     Instantiation();    
 
 }
 
 
 function Update()
 {    
     
      if(Input.GetKeyDown(KeyCode.Space))
      {
          if(currentCube==redCube)
          {
              currentCube=blueCube;
          }
          
          else if(currentCube==blueCube)
          {
              currentCube=redCube;
          }
          
          Instantiation();
          
      }
      
      
 Debug.Log(currentCube.GetComponent(forCube).transform.position.x);
     
     
 }
 
 
 
 function Instantiation()
 {
         Instantiate(currentCube, Vector3(0,0,0),Quaternion.identity); 
         
 }

There is one more code, which is attached to cubes. And that is:-

 var flag: boolean = false;
 var control:boolean=true;
 
 function Update () 
 {
 if(control)
 {
 
 if (Input.GetKeyUp(KeyCode.LeftArrow)&&transform.position.x>-3)
   {
 //      Debug.Log("left arrow");
       transform.position.x--;
   }
   
   else if
   (Input.GetKeyUp(KeyCode.RightArrow)&& transform.position.x<3)
   {
    transform.position.x++;
   }
   
   if(Input.GetKeyDown(KeyCode.Space))
   {
       flag=true;
       control=false;
   }
   
  } 
   if(flag)
   {
           if(transform.position.y>-5)
           {
           
           transform.position.y--;
           
           }
   }
 
 }

According to my first code the cube get instantiated at 0,0,0 positions but in the log screen it shows -10 for once then 0 then -10 then 0, per instantiation. Can anyone please tell me where have I gone wrong???

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 syclamoth · Mar 12, 2012 at 12:54 PM 0
Share

But... what did you seriously expect? What I can see here is you swapping between the red and the blue cubes, and reporting the x-axis position of one every time you swap. The instantiated object doesn't get taken into account at all. If the red and blue cubes are prefabs, then the position won't mean anything at all.

avatar image ArunChnadran · Mar 12, 2012 at 01:48 PM 0
Share

Here I am trying to make a grid(7x5) filled with red cubes and blue cubes. In-order to find which column the cube should fall I need to know the x-position. can u please suggest something.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ArunChnadran · Mar 20, 2012 at 02:48 PM

The instantiated value dosen't have the instance. This was my problem. Here are the things that I changed/added:-

 var extraCube:GameObject; // a variable to store the Instantiated value.

then, in function Instantiation:-

 function Instantiation()
 {
        extraCube=Instantiate(currentCube, Vector3(0,0,0),Quaternion.identity); 
 
 }


now in the main program:-

 Debug.Log(extraCube.GetComponent(forCube).transform.position.x);
Comment
Add comment · 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
0

Answer by DaveA · Mar 12, 2012 at 11:26 PM

Could you describe your project more? Is it like Tetris or are you pre-populating a grid?

You get the x coord of anything by getting its transform. Ex:

  currentCube.transform.position.x

Your code will create a new red or blue cube at 0,0,0 every frame. My guess is one of your red or blue prefabs has a -10 and the other has 0.

This code:

 currentCube.GetComponent(forCube).transform.position.x

would be better as

 currentCube.transform.position.x

" In-order to find which column the cube should fall I need to know the x-position." x-position of what exactly? Your cubes are either at zero or -10 when created. It really seems like there needs to be more interaction here, like with the mouse or keys or time or something. Creating 60+ cubes per second without stopping, that's a memory leak.

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 ArunChnadran · Mar 13, 2012 at 03:54 AM 0
Share

the game is like four balls. put four adjacent cubes of same colour in vertical or horizontal or diagonal. Then one thing is that it won't create 60+ cube at every frame only one when the space bar is pressed(thats what I saw in the hierarchy). I need to know the x-position of the cube. $$anonymous$$ore about the game, It has 7 columns and 5 rows, red cube get instantiated at 0th position and can move 3 columns left and 3 columns right, when the user press the space bar the cube fall down to the bottom, then another cube gets instantiated (with same features, but blue, player 2).Since the cubes fall on top the other, when the cubes fall it need to check in which row it should stop(y-position). then I'll save values in the grid and compare them and check whether any one had won. Hope you are clear!!

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Create a straight gradient equation with a grid of 3d objects 1 Answer

How do I freeze a gameObject when MouseDrag runs over a certain Vector3? 1 Answer

Create an animation with variables? 1 Answer

not instantiating at correct transform 2 Answers

Object not moving,Object not moving in any direction 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