I can not figure out why the code is not working. It gives me an error “No overload for method ‘lerp’ takes 4 arguments” Here is the code
-
using System.Collections;
-
using System.Collections.Generic;
-
using System.Diagnostics;
-
using UnityEngine;
-
[DebuggerDisplay(“{” + nameof(DebuggerDisplay) + “(),nq}”)]
-
public class CrossHair : MonoBehaviour
-
{
-
[Range(0, 100)]
-
public float Value;
-
public float speed;
-
public float margin;
-
public RectTransform Top, Bottom, Right, Left, Center;
-
void Update()
-
{
-
float TopValue, BottomValue, RightValue, LeftValue;
-
TopValue = Mathf.LerpUnclamped(Top.position.y, Center.position.y, +margin + Value, speed * Time.deltaTime);
-
BottomValue = Mathf.Lerp(Bottom.position.y, Center.position.y - margin - Value, speed * Time.deltaTime);
-
RightValue = Mathf.Lerp(Right.position.x, Center.position.x - margin - Value, speed * Time.deltaTime);
-
LeftValue = Mathf.Lerp(Left.position.x, Center.position.x + margin + Value, speed * Time.deltaTime);
-
Top.position = new Vector2(Top.position.x, TopValue);
-
Bottom.position = new Vector2(Bottom.position.x, BottomValue);
-
Right.position = new Vector2(RightValue, Center.position.y);
-
Left.position = new Vector2(LeftValue, Center.position.y);
-
}
-
public override bool Equals(object obj)
-
{
-
return obj is CrossHair hair &&
-
base.Equals(obj) &&
-
DebuggerDisplay == hair.DebuggerDisplay;
-
}
-
private string DebuggerDisplay => ToString();
-
}
I’m guessing you just made some typing mistakes copying this. You can fix those as follows:
The complete error message contains everything you need to know to fix the error yourself.
The important parts of the error message are:
- the description of the error itself (google this; you are NEVER the first one!)
- the file it occurred in (critical!)
- the line number and character position (the two numbers in parentheses)
- also possibly useful is the stack trace (all the lines of text in the lower console window)
Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.
Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly? Are you structuring the syntax correctly? Look for examples!
All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.
Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.
Please familiarize yourself with the way this forum works (namely how to render your code properly). This is how your code is supposed to look
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
[DebuggerDisplay(“{” + nameof(DebuggerDisplay) + “(),nq}”)]
public class CrossHair : MonoBehaviour
{
[Range(0, 100)]
public float Value;
public float speed;
public float margin;
public RectTransform Top, Bottom, Right, Left, Center;
void Update()
{
float TopValue, BottomValue, RightValue, LeftValue;
TopValue = Mathf.LerpUnclamped(Top.position.y, Center.position.y, +margin + Value, speed * Time.deltaTime);
BottomValue = Mathf.Lerp(Bottom.position.y, Center.position.y - margin - Value, speed * Time.deltaTime);
RightValue = Mathf.Lerp(Right.position.x, Center.position.x - margin - Value, speed * Time.deltaTime);
LeftValue = Mathf.Lerp(Left.position.x, Center.position.x + margin + Value, speed * Time.deltaTime);
Top.position = new Vector2(Top.position.x, TopValue);
Bottom.position = new Vector2(Bottom.position.x, BottomValue);
Right.position = new Vector2(RightValue, Center.position.y);
Left.position = new Vector2(LeftValue, Center.position.y);
}
public override bool Equals(object obj)
{
return obj is CrossHair hair &&
base.Equals(obj) &&
DebuggerDisplay == hair.DebuggerDisplay;
}
private string DebuggerDisplay => ToString();
}
That said, your problem is in line 19 where you have a redundant comma between Center.position.y and +margin + value and thus attempt to call LerpUnclamped with 4 arguments instead of 3.
1 Like
Try this:
TopValue = Mathf.LerpUnclamped(Top.position.y, Center.position.y +margin + Value, speed * Time.deltaTime);
Im very new to Unity and The Unity part of discussions.
Excellent! Welcome. Get to work… there’s a lot of fun ahead for you!
I recommend this approach to understanding the complexities of game development:
Imphenzia: How Did I Learn To Make Games:
Two steps to tutorials and / or example code:
- do them perfectly, to the letter (zero typos, including punctuation and capitalization)
- stop and understand each step to understand what is going on.
If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.