• Register
0 votes
166 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

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 296 views
296 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 79 views
79 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
2 answers 387 views
387 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
1 vote
1 answer 122 views
122 views
Problem : Looking for the solution on bellow problem setting the parent of a transform which resides in a prefab is disabled to prevent data corruption
asked Nov 8, 2019 peterlaw 6.9k points
0 votes
1 answer 1.9K views
1.9K views
Problem: I am currently wondering why I am getting below compile error if I just declare variable with var or let keywords? I want to say, below code goes well: export class MyAppComponent { myRefreshClickStream$: any; constructor(){ } } While this brings the error: export class MyAppComponent { var myRefreshClickStream$: any; constructor(){ } }
asked Jan 27 jwilliam 3.9k points