- Home /
Object position x = to another objects x position.
I have looked around an I am still a bit new to using "position" as I have not really needed to use it before in things I have coded.
I am trying to make the objects x position = to another objects x position.
Here is my scrpt C#
using UnityEngine;
using System.Collections;
public class InLine : MonoBehaviour {
public GameObject Player;
public GameObject Me;
void Update () {
Player.position = new Vector3 (transform.position.x, transform.position.y, transform.position.z);
Me.position = new Vector3 (Player.x, transform.position.y, transform.position.z);
}
}
I get the issues: Vector 3 issues float, float, float. Gameobject has no definition for position and GameObject has no definition for x or extensions of w.e something along those lines.
Any help I would be very grateful.
Thank you,
Answer by jahroy · Dec 13, 2011 at 05:05 AM
Position is a member (or property) of the Transform class.
The variable x is a member (or property) of a transform's position.
To reference the x-value of the position of a transform, you do this:
var otherThing : GameObject;
function Update () { gameObject.transform.position.x = otherThing.transform.position.x; }
Or something like that...
In this case gameObject.transform.position means the position of the transform of the game object that this script is attached to. In other words, you shouldn't need the variable named Me.
I usually work with Transforms rather than GameObjects, but I think it works like that.
Also, variable names should begin with lowercase letters. Capital letters are used for classes, functions, namespaces, etc...
Thank you for that. The small things like that always screw me up. I am program$$anonymous$$g in C# thus why I think I got this error Assets/InLine.cs(10,30): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable Any help with that?
It sounds like the compiler is just confused because (according to what it knows) you're treating members like functions and/or vice versa.
I'm not sure if you're asking me about a separate compiler error or not.
If you are, your best bet is to include the code from your file where the error occurs.
The compiler errors will usually point you towards a specific line number in one of your scripts.
The above error message indicates that something is wrong on line 10 of a file named InLine.cs.
EDIT:
I just took a closer look at your code.
That error message is a result of the fact that you declare the variable named $$anonymous$$e to be a GameObject. You then refer to $$anonymous$$e.position.
This is confusing the compiler because a GameObject does not have a field named position. This is an error. A GameObject has a transform (which has a position).
I guess getting that idea across was the desired goal of my answer.
Fixed it all Cheers mate what you said helped me work out my problem. It was more to do with my converting from JS to C# than what you wrote, Cheers for the understanding though.
Answer by RockingGameDeveloper · Dec 01, 2019 at 07:40 PM
Thank you to the person who knows the difference between JavaScript and C#!
Your answer
Follow this Question
Related Questions
How can I use Vector3, Quaternion Classes in my server? 0 Answers
AddForce to the Left? 3 Answers
What is the problem with this code? 1 Answer
What is wrong with my zipline script? 2 Answers
Vector3.Project, how to? 2 Answers