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 MarwanMR1 · Feb 04, 2012 at 08:50 PM · transformpositionvector3translate

UnityEngine.Component:get_transform() Error

Hi everyone, I am working on a tetris like game. So I have a class called OBlock. It has a 2x2 multidimensional array of prefabs called Block. OBlock doesn't inherit from MonoBehaviour. I use a timer in OBlock to move the blocks in the 2D array one step down.

in OBlock

 private void MoveDownOneBlock()
 {
     OBlocks[0, 0].MoveDownOneBlock(); // Line 151
     OBlocks[0, 1].MoveDownOneBlock();
     OBlocks[1, 0].MoveDownOneBlock();
     OBlocks[1, 1].MoveDownOneBlock();
 }

in Block

 public void MoveDownOneBlock()
 {
     move = -0.5f;
     transform.position = new Vector3(transform.position.x, transform.position.y + move, transform.position.z); // Line 159
     RowNum++;
 }

When I run this code I get the following error.

UnityEngine.Component:get_transform()

Block:MoveDownOneBlock() (at Assets\Scripts\Block.cs:159) OBlock:MoveDownOneBlock() (at Assets\Scripts\OBlock.cs:151) OBlock:Update() (at Assets\Scripts\OBlock.cs:80) OBlock:Timer_Elapsed(Object, ElapsedEventArgs) (at Assets\Scripts\OBlock.cs:202) System.Timers.Timer:Callback(Object) I get the same error even when I do this public void MoveDownOneBlock() { move = -0.5f; transform.Translate(new Vector3(0, move, 0)); RowNum++; } The weird thing is that I was able to change the blocks' location at the start of the code. in OBlock.cs public void init() { OBlocks[0, 0].RowNum = 0 OBlocks[0, 0].ColumnNum = 5; OBlocks[0, 0].InitPosition();
OBlocks[0, 1].RowNum = 0; OBlocks[0, 1].ColumnNum = 6; OBlocks[0, 1].InitPosition(); OBlocks[1, 0].RowNum = 1; OBlocks[1, 0].ColumnNum = 5; OBlocks[1, 0].InitPosition(); OBlocks[1, 1].RowNum = 1; OBlocks[1, 1].ColumnNum = 6; OBlocks[1, 1].InitPosition(); } in Block.cs public void InitPosition() { MaxRight = 3.5f; MaxLeft = -1.5f; MaxUp = 4.7f; MaxDown = -3.3f; transform.position = new Vector3((ColumnNum 0.5f) + MaxLeft, ((RowNum 0.5f) - MaxUp) * -1, transform.position.z); // Line 40 } and no error appeared. So I tried this public void MoveDownOneBlock() { RowNum++; InitPosition(); } but I got the same error pointing to line 40. Can someone help me with this? Sorry for the long question. I want to give you as much details as possible.
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 Kryptos · Feb 04, 2012 at 09:28 PM

I'm pretty sure .Net Timers are executed in their own thread. In Unity it is not possible to manipulate the scene hierarchy (all objects instantiated on the current scene) from outside the main thread.

But I don't understand the need of timers. Unity API already provide everything you need for timed behaviours. You may want to use the Time.deltaTime in an Update() of your script or a coroutine.


With a coroutine:

 bool m_bIsGameFinished = false;
 IEnumerator Start()
 {
     while( !m_bIsGameFinished )
     {
         yield return new WaitForSeconds( 1.0f );
         MoveDownOneBlock();
     }
 }

Inside Update method:

 float m_fElapsedTime = 0.0f;
 void Update()
 {
     this.m_fElapsedTime += Time.deltaTime;
     if( this.m_fElapsedTime >= 1.0f )
     {
         this.m_fElapsedTime = 0.0f;
         MoveDownOneBlock();
     }
 }
Comment
Add comment · Show 4 · 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 MarwanMR1 · Feb 04, 2012 at 09:35 PM 0
Share

I'm using a timer because I want the the blocks to move down every second. Not every frame. Is there is a way to do that without using a timer?

avatar image Kryptos · Feb 04, 2012 at 09:52 PM 0
Share

With a coroutine:

 bool m_bIsGameFinished = false;
 IEnumerator Start()
 {
     while( !m_bIsGameFinished )
     {
         yield return new WaitForSeconds( 1.0f );
         $$anonymous$$oveDownOneBlock();
     }
 }

Inside Update method:

 private float m_fElapsedTime = 0.0f;
 void Update()
 {
     this.m_fElapsedTime += Time.deltaTime;
     if( this.m_fElapsedTime >= 1.0f )
     {
         this.m_fElapsedTime = 0.0f;
         $$anonymous$$oveDownOneBlock();
     }
 }
avatar image MarwanMR1 · Feb 05, 2012 at 06:47 AM 0
Share

Thanks, I'll try that

avatar image MarwanMR1 · Feb 05, 2012 at 11:59 AM 0
Share

It worked perfectly. Thanks alot

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Issue's With Positioning 0 Answers

gravity causes transform.position not to work 0 Answers

Offset against Rotated Object,Applying Offset to a Rotated Transform 1 Answer

Guided missile help c# 1 Answer

Reposition to minimum distance from target 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