• Register
0 votes
357 views

Problem :

I am very new to C#. I have written the below C# code.

void UpdateAxis () {
float myxP = Input.GetAxis ("Horizontal")*Time.deltaTime * 20; 
transform.Translate (Vector3(myxP,0,0));//error is here 
transform.position.x = Mathf.Clamp (transform.position.x, -10, 10);
} 

But I am facing following error while trying to execute the above code.

error : Expression denotes a type', where avariable', value' ormethod group' was expected`

Kindly suggest the solution if any.

6 5 3
7,540 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

I think you are missing the new keyword that is very necessary to call any constructor from C# code. Maybe your code was improperly translated from another language (e.g. your syntax is valid in Python), or you have just written wrongly.

The correct way to write the code is as below:

transform.Translate(new Vector3(myxP, 0, 0));

You must consider writing the code with the Vector3.right instead, to clarify the direction

Please find below the code written using Vector3.right

transform.Translate(Vector3.right * myxP);

 

OR

When you want to use the C# you must use the new keyword in front of all the constructors. The Constructors are like the functions that are used to create new objects . They must have the same name as the type of the object you want to create. So if you want to create a new Rect using your constructor Rect() then you must write new Rect() if you are using C#.

9 7 4
38,600 points

Related questions

0 votes
1 answer 6 views
6 views
Problem: Here is my code. I'm stuck with this recurring error and am at a loss for what to do next. Any guidance on what direction to look in would be great as I am learning how to code. The error message is listed below. Error CS0119: Expression denotes a 'type', where a 'variable', 'value' or 'method ... 1; else c = c * (i - j + 1) / j; Console.Write("{0} ", c); } Console.Write("\n"); } } } }
asked Apr 25 muktaa 34.4k points
1 vote
1 answer 5 views
5 views
Problem; I get the following error: Error: aggregate value used where an integer was expected.
asked 6 days ago PkGuy 23.5k points
0 votes
1 answer 11 views
11 views
Problem: I have a Section of code that is compiling with the error of "Not all code paths Return a Value" I have no idea how to fix this. Any Ideas?
asked Mar 16 ummesalma 29.2k points
0 votes
1 answer 517 views
517 views
Problem : I am trying to create the test app using unity but facing following issue: readpixels was called to read pixels from system frame buffer, while not inside drawing frame.
asked Nov 14, 2019 peterlaw 6.9k points
0 votes
1 answer 17 views
17 views
Problem invalidcastexception: cannot cast from source type to destination type.
asked Feb 11 charles mathews 5.5k points
0 votes
1 answer 1 view
1 view
Problem: I have two Scenes in Unity. First one is for importing data from a binary file and after file is imported i create a gameobject with the coordinates i read from the binary file. I use DontDestroyOnLoad for this game object. Then i load my second scene using ... causes the issue. However, i do not know how to fix this because i must change the parent i can't change that logic.
asked Apr 26 ochena 22.1k points
0 votes
1 answer 189 views
189 views
Problem : I am facing following error with the below query in SQL Server 2012. An expression of non-boolean type specified in a context where a condition is expected, near 'RETURN'. SQL Query: CREATE FUNCTION [dbo].[GetMYPMResources](@UserResourceNo ... JOIN JobTask t ON t.JobTaskNo = m.JobTaskNo WHERE t.TaskManagerNo = @UserResourceNo OR t.AlternateTaskManagerNo = @UserResourceNo ) RETURN END
asked Dec 4, 2019 alecxe 7.5k points
0 votes
1 answer 3 views
3 views
Problem: I want to create dynamically ui button, I have one prefab, here is code: public class SFSConnect : MonoBehaviour { SmartFox sfs; public GameObject cnv1; public GameObject potchPrf; public Canvas fCanvas; void Start () { fCanvas = cnv1.GetComponent<Canvas> ... 10), Quaternion.identity); potchPrf.transform.SetParent (fCanvas.transform); } } I tried thousands of posts, but nothing happened.
asked 4 days ago anika11 32k points
0 votes
1 answer 48 views
48 views
Problem: Can someone help, How to fix &ldquo;Can't Play a Disabled Audio Source&rdquo;? Any help is appreciated :)
asked Mar 16 ummesalma 29.2k points
0 votes
2 answers 777 views
777 views
Problem : I am new to Unity5. I am facing following error while trying to do setDestination. "SetDestination" can only be called on an active agent that has been placed on a NavMesh. UnityEngine.NavMeshAgent:SetDestination(Vector3) CompleteProject.EnemyMovement:Update() (at Assets/ ... ; } } } } May I know what is wrong with the above code? Does anyone have any solution on my issue?
asked Nov 27, 2019 alecxe 7.5k points