- Home /
GameObject position not matching up to its position in the inspector even though they are clearly linked [answered]
I have an list of objects which i want to get to the position of so i scan through the list and get the positions but the position which i get are not the same as the ones that i see in the inspector. This is my problem!
Things i have tried:
I change the colors of said game objects to check the link between inspector and code that worked proving the link was there
I changed the way i set the positions originally from using tranform.postion to transform.localPosition
I tried changing the locations of the game objects in my code in order to check the link between inspector. I then saw the game objects move to the position i set.
When using debug.log to get the position i stored the position in a variable as i know c# can have issues with tranform.position but no change.
So now i am asking you to solve what seems to be to be an impossible problem but as i have only be coding for around 6 months there must be more to know which will enable you to help me solve this problem. I have put my code below and tried to highlighted were the debug message are that are against what i see in the inspector and a screen shot of the inspector.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class PolyDrawExample : MonoBehaviour
{
public int numberOfSides;
public float polygonRadius;
public Vector2 polygonCenter;
public GameObject empty;
public GameObject empty2;
List<GameObject> Lines;
Line Line;
int i;
List<Vector3> Vertices;
List<GameObject> Spheres;
Vector2 currentCorner;
public GameObject button;
public GameObject buttonObj;
new public Camera camera;
public Transform canvas;
Vector3 buttonPressed;
int i2;
List<Vector3> BallPos;
List<GameObject> Buttons;
int i3;
List<Line.line> lineInfos;
public GameObject ball;
List<GameObject> Balls;
List<Vector3> PossibleTargets;
List<Vector3> Targets;
List<int> toDelete;
int numOfBalls;
int i4;
int i5;
bool targeted;
int i6;
int i7;
int i8;
int i9;
int i10;
int i11;
public float speed;
float step;
Vector2 startCorner;
Vector2 previousCorner;
void Start()
{
step = speed * Time.deltaTime;
numberOfSides = 5;
polygonRadius = 2.5f;
i = 1;
polygonCenter = new Vector2(0, 0);
Lines = new List<GameObject>();
Vertices = new List<Vector3>();
Buttons = new List<GameObject>();
lineInfos = new List<Line.line>();
Spheres = new List<GameObject>();
Balls = new List<GameObject>();
PossibleTargets = new List<Vector3>();
Targets = new List<Vector3>();
BallPos = new List<Vector3>();
toDelete = new List<int>();
BallPos = Vertices;
Line = GetComponent<Line>();
GetCorners(polygonCenter, polygonRadius, numberOfSides);
numOfBalls = 3;
SpawnObstacles();
}
void Update()
{
i9 = 0;
while (i9 < Spheres.Count)
{
// HERE HERE HERE HERE HERE HERE HERE HERE HERE HERE HERE HERE HERE HERE
Vector3 posi = Spheres[i9].transform.position;
Debug.Log(posi);
i9 = i9 + 1;
// HERE HERE HERE HERE HERE HERE HERE HERE HERE HERE HERE HERE HERE HERE
}
if (Time.frameCount == 3)
{
DrawAll();
AddIntersectVertices();
}
if (Time.frameCount == 2)
{
CleanVertices();
}
SetBallTargets();
i2 = 0;
i3 = 0;
while (numberOfSides + 1 > i2)
{
if (Buttons[i2].GetComponent<ButtonPressControl>().click == true)
{
//Debug.Log(Buttons[i2].name + " is pressed");
while (i3 < Lines.Count)
{
//Debug.Log("Camera position is " + camera.ScreenToWorldPoint(Buttons[i2].transform.position) + " Line " + (i3 + 1) + " start position is " + lineInfos[i3].startpos + " end position is " + lineInfos[i3].endpos);
if (camera.ScreenToWorldPoint(Buttons[i2].transform.position) == lineInfos[i3].startpos || camera.ScreenToWorldPoint(Buttons[i2].transform.position) == lineInfos[i3].endpos)
{
Lines[i3].GetComponent<LineRenderer>().SetColors(Color.red, Color.red);
}
i3 = i3 + 1;
}
i3 = 0;
while (i3 < Spheres.Count)
{
if (camera.ScreenToWorldPoint(Buttons[i2].transform.position) == Spheres[i3].transform.position)
{
Spheres[i3].GetComponent<Renderer>().material.color = Color.red;
}
i3 = i3 + 1;
}
}
i2 = i2 + 1;
}
}
void GetCorners(Vector2 center, float rad, int numSides)
{
startCorner = new Vector2(rad, 0) + polygonCenter;
previousCorner = startCorner;
for (i = 1; i < numSides; i++)
{
float cornerAngle = 2f * Mathf.PI / (float)numSides * i;
currentCorner = new Vector2(Mathf.Cos(cornerAngle) * rad, Mathf.Sin(cornerAngle) * rad) + polygonCenter;
//Debug.Log(i + " " + previousCorner + " " + currentCorner);
// Having used the current corner, it now becomes the previous corner.
previousCorner = currentCorner;
Vertices.Add(previousCorner);
}
// lines list capacity is 4
// Draw the final side by connecting the last corner to the starting corner.
Vertices.Add(startCorner);
i = 1;
while(i < numSides + 1)
{
GameObject sphere2 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere2.transform.localPosition = Vertices[i - 1];
sphere2.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
sphere2.GetComponent<Renderer>().material = new Material(Shader.Find("GUI/Text Shader"));
sphere2.GetComponent<Renderer>().material.color = Color.blue;
sphere2.name = "Sphere " + i;
Spheres.Add(sphere2);
i = i + 1;
}
Vertices.Add(Vector3.zero);
i = 1;
while (i < numSides + 2)
{
GameObject Button = Instantiate(button) as GameObject;
Button.transform.SetParent(canvas, false);
Button.transform.position = camera.WorldToScreenPoint(Vertices[i - 1]);
Button.name = "Button " + i;
Buttons.Add(Button);
i = i + 1;
}
}
void SpawnObstacles()
{
while (numOfBalls > i4)
{
i5 = Random.Range(0, BallPos.Count);
GameObject Ball = Instantiate(ball) as GameObject;
Ball.transform.position = BallPos[i5];
Ball.transform.position = new Vector3(Ball.transform.position.x, Ball.transform.position.y, -1);
Ball.name = "Ball " + (i4 + 1);
Balls.Add(Ball);
BallPos.RemoveAt(i5);
i4 = i4 + 1;
}
}
void SetBallTargets()
{
i6 = 0;
i7 = 0;
i8 = 0;
if (targeted == false)
// set intial targets for the balls
{
PossibleTargets = Vertices;
while(i6 < Balls.Count)
// do it for every ball
{
i7 = Random.Range(0, PossibleTargets.Count);
// the random target for this ball
Targets.Add(PossibleTargets[i7]);
//add the target to list of the selected targets
PossibleTargets.RemoveAt(i7);
// remove the chosen target from targets to chose so that you do not pick the same target twice
i6 = i6 + 1;
}
if (i6 == Balls.Count)
{
targeted = true;
// when all balls have had targets created
}
}
if (targeted == true)
{
while(i8 < Balls.Count)
// do it for every ball
{
Balls[i8].transform.position = Vector3.MoveTowards(Balls[i8].transform.position, Targets[i8], step);
// each ball move towards the targets set
i8 = i8 + 1;
}
}
}
void DrawLine(Vector3 start, Vector3 end)
{
GameObject li = Instantiate(empty) as GameObject;
// create empty
Lines.Add(li);
// add to lines gameobect list
Line.line linee = new Line.line(Lines[Lines.Count - 1], start, end, Color.blue);
// create a line from class from other script
lineInfos.Add(linee);
// add Line.line to list
Line.setupline(lineInfos[lineInfos.Count - 1]);
// attch line renderer to game object
li.name = "Line " + Lines.Count;
// name
}
void CleanVertices ()
{
i9 = 0;
while (i9 < Spheres.Count)
{
Spheres[i9].GetComponent<Renderer>().material.color = Color.green;
Spheres[i9].transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
Debug.Log(Spheres[i9].transform.position);
i9 = i9 + 1;
}
i9 = 0;
while (i9 < Spheres.Count)
{
Vertices.Insert(i9, Spheres[i9].transform.position);
i9 = i9 + 1;
}
i9 = 0;
while (i9 < Vertices.Count)
{
Debug.Log(i9 + " " + Vertices[i9]);
i9 = i9 + 1;
}
}
void DrawAll()
{
Debug.Log(Vertices.Count);
i9 = 0;
while (i9 < Vertices.Count)
{
Debug.Log(i9 + " " + Vertices[i9]);
i9 = i9 + 1;
}
while (i10 < numberOfSides)
{
i11 = 0;
while (i11 < numberOfSides)
{
DrawLine(Vertices[i10], Vertices[i11]);
i11 = i11 + 1;
}
i10 = i10 + 1;
}
}
void AddIntersectVertices ()
{
GameObject sphere2 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere2.transform.position = FindIntersect(lineInfos[3].startpos, lineInfos[3].endpos, lineInfos[9].startpos, lineInfos[9].endpos);
sphere2.transform.localScale = new Vector3(0.4f, 0.4f, 0.4f);
sphere2.GetComponent<Renderer>().material = new Material(Shader.Find("GUI/Text Shader"));
sphere2.GetComponent<Renderer>().material.color = Color.blue;
lineInfos[3].colour = Color.red;
Line.setColor(lineInfos[3].gameobject.GetComponent<LineRenderer>(), Color.red);
lineInfos[9].colour = Color.red;
Line.setColor(lineInfos[9].gameobject.GetComponent<LineRenderer>(), Color.red);
}
Vector3 FindIntersect(Vector2 xy1, Vector2 xy2, Vector2 xy3, Vector2 xy4)
{
Debug.Log("line 4 starts at " + xy1 + " and ends at" + xy2 + "line 10 starts at " + xy3 + " and ends at " + xy4);
float AGradient = (xy2.y - xy1.y) / (xy2.x - xy1.x);
float BGradient = (xy4.y - xy3.y) / (xy4.x - xy3.x);
float AYIntercept = xy1.y - (AGradient * xy1.x);
float BYIntercept = xy3.y - (BGradient * xy2.x);
Vector2 ABIntercept = Vector2.zero;
ABIntercept.x = (BYIntercept - AYIntercept) / (AGradient - BGradient);
ABIntercept.y = (AGradient * ABIntercept.x) + AYIntercept;
return (ABIntercept);
}
}
[1]: /storage/temp/64805-error.png
If needed i can add the exact values displayed in the inspector compared to debug messages
Answer by Frank126 · Feb 27, 2016 at 03:36 PM
I dont really get why you are trying to debug those but I think you are debuging those vector3 to see what is the problem when you compare them to each other and they are never the same (line 105 and 113 of your code).
First, the Vector3 debug only show 1 decimal place for simplicity reason.
The problem you have is that you are trying to compare 2 Vector3 which are made of floats. To compare floats you must use Mathf.Approximately.
You could also compare those Vector3 with Vector3.Distance and see if the distance is, for exemple, 0.1f or less.
Hope it helps.