- Home /
transform.position reads 0 but the actual position is different
Hi,
I'm trying to read the position of a gameobject where a script placed on, but the reading always stays (0.0,0.0,0.0)
, While the actual position of the object is different
This is the Pattern
script that is reading the position
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public struct ShotVariables
{
public float Speed;
public int Direction;
}
public class PatternPoint
{
public List<ShotVariables> Shots = new List<ShotVariables>();
public int Time;
public PatternPoint(int time)
{
Time = time;
}
public void NewShot(int direction, float speed)
{
ShotVariables _shot_variables = new ShotVariables();
_shot_variables.Direction = direction;
_shot_variables.Speed = speed;
Shots.Add(_shot_variables);
}
}
public class Pattern : MonoBehaviour
{
[SerializeField]
public List<PatternPoint> PatternPoints = new List<PatternPoint>();
public GameObject Shot;
public Vector3 Position;
private int ShotIndex;
private int Counter;
public void Wake()
{
ShotIndex = 0;
Counter = 0;
Debug.Log(this.transform.position);
InvokeRepeating("Shoot", 1.0f, 0.1f);
}
public PatternPoint NewPatternPoint(int Time)
{
PatternPoint _patternPoint = new PatternPoint(Time);
return _patternPoint;
}
private void Shoot()
{
Position = this.transform.position;
Debug.Log(Position);
if (ShotIndex < PatternPoints.Count)
{
if (PatternPoints[ShotIndex].Time == Counter)
{
for (int i = 0; i < PatternPoints[ShotIndex].Shots.Count; i++)
{
Debug.Log(Position);
GameObject _Shot = Instantiate(Shot, Position, Quaternion.identity) as GameObject;
Shot _shot_script = _Shot.GetComponent<Shot>();
_shot_script.SetDirection(PatternPoints[ShotIndex].Shots[i].Direction);
_shot_script.Speed = PatternPoints[ShotIndex].Shots[i].Speed;
}
ShotIndex++;
}
Counter++;
}
else
{
CancelInvoke();
}
}
}
The Debug.Log statement on line 69 is reading (0.0,0.0,0.0)
, which should be (2.0, 1.0, 0.0)
which is the actual position in the inspector
This object is instantiated in another script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimingPoint
{
public int Time;
public GameObject Enemy;
public Enemy enemyScript;
public float StartX;
public Vector2 StallMarker;
public float ApproachSpeed;
public TimingPoint(int newTime, GameObject newEnemy, float startX, Vector2 endMarker, float approachSpeed)
{
StartX = startX;
StallMarker = endMarker;
ApproachSpeed = approachSpeed;
Time = newTime;
Enemy = newEnemy;
}
}
public class MapPlay : MonoBehaviour
{
public List<TimingPoint> TimingPoints = new List<TimingPoint>();
public GameObject Enemy;
public int MapLength;
private int enemyIndex;
private int Counter;
private void Awake ()
{
enemyIndex = 0;
Counter = 0;
TimingPoint TP1 = new TimingPoint(10, Enemy, 2f, new Vector2(2f, 1f), 7f);
Pattern P1 = TP1.Enemy.GetComponent<Pattern>();
PatternPoint PP1 = P1.NewPatternPoint(1);
PP1.NewShot(180, 0.01f);
PP1.NewShot(170, 0.01f);
PP1.NewShot(190, 0.01f);
P1.PatternPoints.Add(PP1);
TimingPoints.Add(TP1);
InvokeRepeating("PlaceEnemies", 3.0f, 0.1f);
}
private void PlaceEnemies()
{
if ((enemyIndex <= TimingPoints.Count - 1))
{
if (TimingPoints[enemyIndex].Time == Counter)
{
GameObject _enemy = Instantiate(TimingPoints[enemyIndex].Enemy, transform.position, Quaternion.identity) as GameObject;
Enemy _enemy_script = _enemy.GetComponent<Enemy>();
_enemy_script.StartX = TimingPoints[enemyIndex].StartX;
_enemy_script.StallMarker = TimingPoints[enemyIndex].StallMarker;
_enemy_script.ApproachSpeed = TimingPoints[enemyIndex].ApproachSpeed;
TimingPoints[enemyIndex].Enemy.GetComponent<Pattern>().Wake();
enemyIndex++;
}
}
else
{
CancelInvoke();
}
Counter++;
}
}
The objects with a Pattern
script attached are instantiated on line 61
The Wake() method in the Pattern
script is called from the MapPlay
class. But strange thing is, that if I try to read the position in an Update()
method in the Pattern
class, the readings are accurate.
I'm sure I am doing something wrong here which I can't seem to figure out.
I hope someone can see the obvious mistake I made. I know the code is not good, but I'm trying to make this work somehow.
If i'm not clear enough, i'm sorry, I tried my best to explain the issue.
Thank you in advance!
Answer by Buckslice · Nov 30, 2017 at 11:14 PM
In MapPlay's PlaceEnemies() function you are calling the Wake() of the original prefab not the instantiated instance. You should replace this line
TimingPoints[enemyIndex].Enemy.GetComponent<Pattern>().Wake();
with
_enemy.GetComponent<Pattern>().Wake();
Your answer
![](https://koobas.hobune.stream/wayback/20220612143718im_/https://answers.unity.com/themes/thub/images/avi.jpg)