How to move 2d charector when another object is pressed

Hi, im new to unity so please make your answer simple, thanks. I have a sprite who is my player , I want to make Button sprites on the screen so when a person clicks them with a mouse he moves, for some reason i when i press the Button sprites the player sprite only moves right and not left.
Here is the code for the player

using UnityEngine;
using System.Collections;

public class player : MonoBehaviour {
	public Vector3 initvel; 
	// Update is called once per frame
	void Update () {
			
		this.rigidbody2D.velocity = initvel ; 

	}
}

Here is the left button sprite

using UnityEngine;
using System.Collections;

public class screen_touch_left : MonoBehaviour {
	void Update () {
		player.FindObjectOfType<player>().initvel.x = 0 ;
		if (Input.GetMouseButtonDown(0) ) {
			
			Vector3 mouseWorldPostion = Camera.main.ScreenToWorldPoint(Input.mousePosition);
			Vector2 mousePos2D = new Vector2(mouseWorldPostion.x, mouseWorldPostion.y);
			Vector2 direction = Vector2.zero; 
			
			//ray cast postion and direction. 
			RaycastHit2D hit =  Physics2D.Raycast(mousePos2D,direction);
			if(hit != null && hit.collider != null){
				//we clicked on something that has a collider
				if(hit.collider.rigidbody2D != null){
					player.FindObjectOfType<player>().initvel.x = -15 ; 
					
				}
			} 
		}
	}
}

Here is the right button sprite

using UnityEngine;
using System.Collections;

public class screen_touch_left : MonoBehaviour {
	void Update () {
		player.FindObjectOfType<player>().initvel.x = 0 ;
		if (Input.GetMouseButtonDown(0) ) {
			
			Vector3 mouseWorldPostion = Camera.main.ScreenToWorldPoint(Input.mousePosition);
			Vector2 mousePos2D = new Vector2(mouseWorldPostion.x, mouseWorldPostion.y);
			Vector2 direction = Vector2.zero; 
			
			//ray cast postion and direction. 
			RaycastHit2D hit =  Physics2D.Raycast(mousePos2D,direction);
			if(hit != null && hit.collider != null){
				//we clicked on something that has a collider
				if(hit.collider.rigidbody2D != null){
					player.FindObjectOfType<player>().initvel.x = 15 ; 
					
				}
			} 
		}
	}
}

Does the following line compile for you?

player.FindObjectOfType<player>().initvel.x = 15 ; 

Changing a member of a struct should not work. I won’t go into detail, since I will suggest another way to fix this, but if you’re curious read up on this. That problem aside, you need to rework this a little bit. This is what your code is currently doing.

  • On each Update cycle, you have two scripts (one on each button) that are checking to see if mouse button 0 is pressed.
  • When that button is pressed, we cast a ray and see if we hit anything with a collider.
  • If we hit something, set the player velocity to 15 (or -15)
  • Since we have two scripts…this is done again (in the second script) but instead sets the value to -15 (or 15)

Here’s a proposed fix, assuming you are using the new UI system:

  • Have one script, call it ButtonInputManager
  • Within ButtonInputManager, define MoveLeft() and MoveRight()
  • MoveLeft() will set the velocity to Vector3.right * -15
  • MoveRight() will set the velocity to Vector3.right * 15
  • For both buttons, set OnClick (in the Inspector) to call ButtonInputManager.MoveRight() or ButtonInputManager.MoveLeft()

With this new solution, we no longer need to manually check if the user is clicking a button, that will be handled for us. Also, now each button has a specific task, to either MoveLeft or MoveRight. Last, within the Move methods we use Vector3.right * aNumber because we cannot modify a member of initVel, we have to set initVel to a new Vector3 which this would accomplish.