- Home /
Why coordinates of instantiated object doesn't change?
Hello, I'm new to Unity and I'd like to get any help with my issue.
I have a gnome prefab object and I want to get coordinate of this object when gnome touch other objects in the scene. But coordinates of gnome are always the same as starting point. Here is how I create gnome in the scene from prefab
GameObject newGnome = (GameObject) Instantiate (gnomePrefab, startingPoint.transform.position, Quaternion.identity);
When gnome touch some trap, I want to get coordinates of this.
Debug.Log(newGnome.transform.position.y);
But I always get the same starting point coordinates. Why? Here is gnome prefab settings
UPDATE1 Here is the script SignalOnTouch. I attach this script to every trap.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
[RequireComponent (typeof (Collider2D))]
public class SignalOnTouch : MonoBehaviour {
public UnityEvent onTouch;
public bool playAudio;
private void OnCollisionEnter2D (Collision2D other) {
SendSignal (other.gameObject);
}
private void OnTriggerEnter2D (Collider2D other) {
SendSignal (other.gameObject);
}
public void SendSignal (GameObject gameObject) {
if (gameObject.tag == "Player") {
onTouch.Invoke ();
}
}
}
And in Unity I setup events which I want to call on touch
Here is the code of GameManager script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : Singleton<GameManager> {
public GameObject startingPoint;
public GameObject gnomePrefab;
private GameObject newGnome;
void Start () {
Reset ();
}
void Update () {
}
public void Reset () {
CreateNewGnome ();
Time.timeScale = 1.0f;
}
void CreateNewGnome () {
RemoveGnome ();
newGnome = (GameObject) Instantiate (gnomePrefab,
startingPoint.transform.position, Quaternion.identity);
}
void KillGnome (Gnome.DamageType damageType) {
Debug.Log(newGnome.transform.position.y);
}
public void TrapTouched () {
KillGnome (Gnome.DamageType.Slicing);
}
}
Gnome is connected to rope. On press down button, rope lenth increased and gnome moved down. Here is the full source of RopeScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RopeScript : MonoBehaviour {
public GameObject ropeSegmentPrefab;
List<GameObject> ropeSegments = new List<GameObject> ();
public bool isIncreasing { get; set; }
public bool isDecreasing { get; set; }
public Rigidbody2D connectedObject;
public float maxRopeSegmentLength = 1.0f;
public float ropeSpeed = 4.0f;
LineRenderer lineRenderer;
// Start is called before the first frame update
void Start () {
lineRenderer = GetComponent<LineRenderer> ();
ResetLength (); // TODO remove ?
}
void Update () {
GameObject topSegment = ropeSegments[0];
SpringJoint2D topSegmentJoint = topSegment.GetComponent<SpringJoint2D> ();
if (isIncreasing) {
if (topSegmentJoint.distance >= maxRopeSegmentLength) {
CreateRopeSegment ();
} else {
topSegmentJoint.distance += ropeSpeed * Time.deltaTime;
}
}
if (isDecreasing) {
if (topSegmentJoint.distance <= 0.005f) {
RemoveRopeSegment ();
} else {
topSegmentJoint.distance -= ropeSpeed *
Time.deltaTime;
}
}
if (lineRenderer != null) {
lineRenderer.positionCount = ropeSegments.Count + 2;
lineRenderer.SetPosition (0, this.transform.position);
for (int i = 0; i < ropeSegments.Count; i++) {
lineRenderer.SetPosition (i + 1,
ropeSegments[i].transform.position);
}
SpringJoint2D connectedObjectJoint = connectedObject.GetComponent<SpringJoint2D> ();
lineRenderer.SetPosition (ropeSegments.Count + 1, connectedObject.transform.TransformPoint (connectedObjectJoint.anchor));
}
}
public void ResetLength () {
foreach (GameObject segment in ropeSegments) {
Destroy (segment);
}
ropeSegments = new List<GameObject> ();
isDecreasing = false;
isIncreasing = false;
CreateRopeSegment ();
}
void CreateRopeSegment () {
GameObject segment = (GameObject) Instantiate (ropeSegmentPrefab, this.transform.position, Quaternion.identity);
segment.transform.SetParent (this.transform, true);
Rigidbody2D segmentBody = segment.GetComponent<Rigidbody2D> ();
SpringJoint2D segmentJoint = segment.GetComponent<SpringJoint2D> ();
if (segmentBody == null || segmentJoint == null) {
Debug.LogError ("Rope segment body prefab has no " + "Rigidbody2D and/or SpringJoint2D!");
return;
}
ropeSegments.Insert (0, segment);
if (ropeSegments.Count == 1) {
SpringJoint2D connectedObjectJoint = connectedObject.GetComponent<SpringJoint2D> ();
connectedObjectJoint.connectedBody = segmentBody;
connectedObjectJoint.distance = 0.1f;
segmentJoint.distance = maxRopeSegmentLength;
} else {
GameObject nextSegment = ropeSegments[1];
SpringJoint2D nextSegmentJoint = nextSegment.GetComponent<SpringJoint2D> ();
nextSegmentJoint.connectedBody = segmentBody;
segmentJoint.distance = 0.0f;
}
segmentJoint.connectedBody = this.GetComponent<Rigidbody2D> ();
}
void RemoveRopeSegment () {
if (ropeSegments.Count < 2) {
return;
}
GameObject topSegment = ropeSegments[0];
GameObject nextSegment = ropeSegments[1];
SpringJoint2D nextSegmentJoint =
nextSegment.GetComponent<SpringJoint2D> ();
nextSegmentJoint.connectedBody =
this.GetComponent<Rigidbody2D> ();
ropeSegments.RemoveAt (0);
Destroy (topSegment);
}
}
hmmmm, can yuou share the full code? since there is something weird in the way you are doing it, where do you have the debug line, inside the oncollision event? if thats the case i would bet you have the GameObject newGnome delcared twice,
We need a bit more info. How do you move the gnome? Share some more script.
can you log in the update the newGnome positiion and check if its being updated? is the gnome child of the rope?
It always the same -0.62 gnome is not child of the rope, should it be child? rope and gnome connected with SpringJoint2D
In the Game$$anonymous$$anager class you call a function RemoveGnome which doesn't appear to exist.
@Bonfire-Boy but his Game$$anonymous$$anager class inherits from his Singleton class which likely contains that function. Anyhow, I don't see where the problem lies going off what you provided, we're still missing something here, but I'm with @xxmariofer, you should check if the position is updating each frame
@highpockets Yes of course, silly of me. What self-respecting Singleton base class would be without a RemoveGnome function ;)
I cleaned up code for you from methods which doesn't impact on movement and on appearing gnome in the scene. I just forgot to remove here
I think you need more informative comments. Just logging a position isn't telling you what you need to know. For example if you change...
Debug.Log(newGnome.transform.position.y);
...to...
Debug.LogFormat(this, "Gnome {0}-{1} is at {2}", newGnome.transform.PathInHierarchy(), gameObject.GetInstanceID(), newGnome.transform.position.y);
...then you'll be able to see for yourself if you've got multiple gnomes
If you don't already have a hierarchy-path function, you'll also need this for the above...
public static class TransformExtensions
{
public static string GetPathInHierarchy(this Transform t)
{
Transform parent = t.parent;
if (parent == null)
{
return t.name;
}
return parent.GetPathInHierarchy() + "/" + t.name;
}
}
Answer by JonPQ · Mar 25, 2019 at 03:57 PM
With animated objects... you'll need to check which bone in the hierarchy the collider(s) is attached to. Animations are usually delivered in 2 ways. 1) walking and running animations... dont move the character.... and code moves the root. 2) the animation moves a child object away from the root, but the root stays still. version 2 can cause collision problems like you are describing...
If collider is on the root of the object... perhaps the gnome is animated away from the root position with other bones as in option 2 above.... if so, the collider (on the root) won't move with the gnome.
So check that first, also check how the root moves compared with the gnome in scene view. If so, then attach to hips or something further down the hierarchy.
You could also put simple box and sphere colliders on key bones in the gnome to use as triggers for when gnome touches something. (if you need to get more accurate than capsule collider)
If your collisions are working, but the co-ordinates are static, keep a reference to a transform on the gnome such as abdomen or hips, not the object root. then use the world position of that object ( not local) after detecting the collision. Another option, if your code is using functions.... OnCollisionEnter... that passes in the collision information to you, so you can use collision.point which is exactly the collision point (or array of collision points)
hello, the quesstion is already answered, the answer is posted in the commments, @gilles14 please convert your cimment to an answer and mark it as correct
thank you for so detailed answer, I already fixed the problem. But thanks anyway
Answer by gilles14 · Mar 25, 2019 at 03:17 PM
finally, I got it. Rope with gnome was connected by body of the gnome in GameManeger script
public RopeScript rope;
rope.connectedObject = currentGnome.ropeBody;
Coordinates of ropeBody are correct. Thank you guys for suggestions and help! @xxmariofer @highpockets @Bonfire-Boy
Your answer
![](https://koobas.hobune.stream/wayback/20220612194233im_/https://answers.unity.com/themes/thub/images/avi.jpg)