-1

I have two scripts like this:

void Update()
{
    if (Input.GetMouseButton(0))
    {
        if (GameObject.FindGameObjectWithTag("Object").transform.position.x > -1.9)
        {
            GameObject.FindGameObjectWithTag("Object").transform.Translate(Vector2.left * szybkosc);
        }
    }
}

And second script is similiar, only there is "Vector2.right" difference. If I have one script, it works perfectly, but when I add second script, they doesn't work. I add these scripts to two different gameobjects.

Programmer
  • 121,791
  • 22
  • 236
  • 328
Sleeper
  • 65
  • 1
  • 8

1 Answers1

1

And second script is similiar, only there is "Vector2.right" difference

That's the problem right there.

You are moving the object left with ("Object").transform.Translate(Vector2.left * szybkosc);

then you have another script that is moving it right with ("Object").transform.Translate(Vector2.right * szybkosc);.

What do you expect to happen? I think that the "Object" GameObject should remain still or even create a weird behavior. You can't be moving one Object to two opposite directions at the-same time.

Maybe the second script is supposed to be moving another GameObject GameObject.FindGameObjectWithTag("AnotherObject") instead of the first GameObject the first script is already moving....


Not related to your problem but you should call GameObject.FindGameObjectWithTag once in the Start function.

GameObject objToMove;

void Start()
{
    objToMove = GameObject.FindGameObjectWithTag("Object");
}

void Update()
{
    if (Input.GetMouseButton(0))
    {
        if (objToMove.transform.position.x > -1.9)
        {
            objToMove.transform.Translate(Vector2.left * szybkosc);
        }
    }
}

EDIT:

I'm making control system. I have two arrows - left arrow and right arrow. When I touch and hold left arrow, object moves left, when I touch and hold right arrow, object moves right.

It's good to mention what you are doing in your question. This is what EventSystems is used for. OnPointerClick, OnDrag and OnEndDrag are used to make such things. You can find more about this here.

In fact, you need a Visual Joystick. Use Unity's CrossPlatformInputManager from the Asset store and that should do it. It will save you so much time of having to make your own with OnPointerClick and OnDrag .

The value from CrossPlatformInputManager.GetAxisRaw("Horizontal") can then be used to move your Object left or right.

Petr Varyagin
  • 287
  • 3
  • 6
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • No, I'm making control system. I have two arrows - left arrow and right arrow. When I touch and hold left arrow, object moves left, when I touch and hold right arrow, object moves right. – Sleeper Apr 18 '17 at 09:13
  • **Yes**. My answer is telling you why it's not moving. Read that again. This is not how to detect touch on Objects. See [here](http://stackoverflow.com/questions/41391708/how-to-detect-events-on-ui-and-gameobjects-with-the-new-eventsystem-api/41392130#41392130) for how to do that. – Programmer Apr 18 '17 at 09:15
  • What I think OPs issue is that he doesn't know that Input.GetMouseButton(0) doesn't care _where_ you click, only _that_ you have clicked, meaning every time you click (doesn't matter where) you will tell the object to move both left and right. – Fredrik Schön Apr 18 '17 at 09:17
  • Yup that what I said in my answer. The thing is moving left and right at the-same time making it to remain still at the-same place. That's why I provided a link in the comment for how to detect touch on any object, – Programmer Apr 18 '17 at 09:25
  • @Sleeper I noticed you use the `CrossPlatformInputManager.GetAxisRaw` in your new question. Don't forget to accept answer if your issue is solved. – Programmer Apr 18 '17 at 11:35
  • @Programmer done, if u can, please help me in my new question – Sleeper Apr 18 '17 at 11:39