Error CS1031

I know there are a lot of these posts out there, but they all seem to be specific to certain instances, not anywhere looking like mine.

The errors specific;
Assets/Script/BoundarySetting.cs(11,26): error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer
Assets/Script/BoundarySetting.cs(12,34): error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer
Assets/Script/BoundarySetting.cs(13,34): error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer
Assets/Script/BoundarySetting.cs(14,34): error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer

The code I wrote;

using UnityEngine;
using System.Collections;

public class BoundarySetting : MonoBehaviour {

	public float edgexMin, edgexMax;
	public float edgeyMin, edgeyMax;
	
	// Update is called once per frame
	void Update () {
	edgexMin = Mathf.Clamp = new Vector3 (-9.4f, 0.0f, 0.0f);
		edgexMax = Mathf.Clamp = new Vector3 (9.7f, 3.8f, 0.0f);
		edgeyMin = Mathf.Clamp = new Vector3 (9.5f, -3.8f, 0.0f);
		edgeyMax = Mathf.Clamp = new Vector3 (9.79f, -0.55f, 0.0f);
	
	}
}

I am trying to make it so my dart sprite does not leave the canvas.

I know I’ve said this before but ANY help would be appreciated. I tried and tried using the wonderful new space shooter tutorial, he did a nice C# snippet about Mathf.Clamp but it doesn’t seem to work in Unity2D.
Thanks in advance folks.

Look up any example of Math.Clamp and compare to what you wrote. Clamp is a function, which is a common programming thing. Lap, MoveTowards ... are also functions. Read about how function calls and parameters work (just any book about programming. Non-Unity C# books and web pages have the best explanations.)

2 Answers

2

Maybe you want something like this? (I don’t know how you are getting the canvas coords right now)

GameObject sprite = Find("Dart");
void Update () {
    Vector2 currentPosition = sprite.transform.position;
    // Reset location if over bounds
    currentPosition.x = Mathf.Clamp(currentPosition.x, lowerLimitX, upperLimitX);
    currentPosition.y = Mathf.Clamp(currentPosition.y, lowerLimitY, upperLimitY);
    sprite.transform.position = currentPosition;
}

You could also look at the Unity class Bounds which might be helpful (use direct or extend it)

Thanks Owen, am looking now.

I can’t reply to your comment Martin, so I will comment here.

I got the coordinates I wanted by positioning the sprite I don’t want to leave the canvas as far to the top, bottom and sides as I want it to go (got that idea from the space shooter tutorial) and entering those coordinates in the xMin, xMax, yMin and yMax.