x


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 AssetsScriptsBlock.cs:159) OBlock:MoveDownOneBlock() (at AssetsScriptsOBlock.cs:151) OBlock:Update() (at AssetsScriptsOBlock.cs:80) OBlock:Timer_Elapsed(Object, ElapsedEventArgs) (at AssetsScriptsOBlock.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.

more ▼

asked Feb 04 '12 at 08:50 PM

MarwanMR1 gravatar image

MarwanMR1
1 4 4 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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();
    }
}
more ▼

answered Feb 04 '12 at 09:28 PM

Kryptos gravatar image

Kryptos
7.2k 5 33

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?

Feb 04 '12 at 09:35 PM MarwanMR1

With a coroutine:

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

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;
        MoveDownOneBlock();
    }
}
Feb 04 '12 at 09:52 PM Kryptos

Thanks, I'll try that

Feb 05 '12 at 06:47 AM MarwanMR1

It worked perfectly. Thanks alot

Feb 05 '12 at 11:59 AM MarwanMR1
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1330
x915
x599
x223

asked: Feb 04 '12 at 08:50 PM

Seen: 729 times

Last Updated: Feb 05 '12 at 11:59 AM