(UNSOLVED) Mathf.Clamp to screen help

I’ve been at this a few days. and others have told me to try certain things and nothing has worked so far. This is my first game and what I’m trying to do is use the accelerometer to move my gameobject along the x axis just left and right. I finally figured out how to do that but I can’t get Mathf.Clamp to clamp my code to the screen size.
If anyone can solve this problem it would make my life. I’m new to Unity and coding so that’s why its going over my head.

using UnityEngine;
using System.Collections;

public class HatController : MonoBehaviour {

public Camera cam;

private float maxWidth;
private bool canControl;

// Use this for initialization
void Start () {
if (cam == null) {
cam = Camera.main;
}
canControl = false;
Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
maxWidth = targetWidth.x;
}

// Update is called once per frame
void Update () {
if (canControl) {
transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z);
Vector3 rawPosition = Input.acceleration;
Vector3 targetPosition = new Vector3 (rawPosition.x , 0.0f, 0.0f);
float targetWidth = Mathf.Clamp (targetPosition.x, -maxWidth, maxWidth);
targetPosition = new Vector3 (targetWidth, targetPosition.y, targetPosition.z);

}
}
public void ToggleControl (bool toggle) {
canControl = toggle;
}
}

You dont seem to be writing back the clamped value to the transform, ie transform.position = targetPosition;

So how would I go about that? in line 28 do I add something?

I use matfh.clamp in this way

//Lock the position in the screen by putting a boundaries
        rigidbody2D.position = new Vector2
            (
                Mathf.Clamp (rigidbody2D.position.x, boundary.xMin, boundary.xMax),  //X
                Mathf.Clamp (rigidbody2D.position.y, boundary.yMin, boundary.yMax)     //Y
            );
    }

I making a 2d game but the idea is the same

So do I delete this line? Either way I will let you know if it works when I get home from work. Thank you!

You should replace the 2d definition with rigidboy and i think that You use Vector3

So I changed the code around and its giving me this error
Assets/scripts/HatController.cs(29,34): error CS1729: The type UnityEngine.Vector2' does not contain a constructor that takes 1’ arguments

this is my new code btw

using UnityEngine;
using System.Collections;

public class HatController : MonoBehaviour {

    public Camera cam;

    private float maxWidth;
    private bool canControl;
    public float xMin, xMax;
    // Use this for initialization
    void Start () {
        if (cam == null) {
            cam = Camera.main;
        }
        canControl = false;
        Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
        Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
        maxWidth = targetWidth.x;
    }
   
    // Update is called once per frame
    void Update () {
        if (canControl) {
            transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z);
            Vector3 rawPosition = Input.acceleration;
            transform.position = new Vector2(
                Mathf.Clamp (transform.position.x, xMin, xMax)
                );
       
    }
}
    public void ToggleControl (bool toggle) {
        canControl = toggle;
    }
}