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
[135283-screenshot-1.png*|135283]
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
[135284-screenshot-4.png*|135284]
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*.transform.position);*
}
SpringJoint2D connectedObjectJoint = connectedObject.GetComponent ();
lineRenderer.SetPosition (ropeSegments.Count + 1, connectedObject.transform.TransformPoint (connectedObjectJoint.anchor));
}
}
public void ResetLength () {
foreach (GameObject segment in ropeSegments) {
Destroy (segment);
}
ropeSegments = new List ();
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 ();
SpringJoint2D segmentJoint = segment.GetComponent ();
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 ();
connectedObjectJoint.connectedBody = segmentBody;
connectedObjectJoint.distance = 0.1f;
segmentJoint.distance = maxRopeSegmentLength;
} else {
GameObject nextSegment = ropeSegments[1];
SpringJoint2D nextSegmentJoint = nextSegment.GetComponent ();
nextSegmentJoint.connectedBody = segmentBody;
segmentJoint.distance = 0.0f;
}
segmentJoint.connectedBody = this.GetComponent ();
}
void RemoveRopeSegment () {
if (ropeSegments.Count < 2) {
return;
}
GameObject topSegment = ropeSegments[0];
GameObject nextSegment = ropeSegments[1];
SpringJoint2D nextSegmentJoint =
nextSegment.GetComponent ();
nextSegmentJoint.connectedBody =
this.GetComponent ();
ropeSegments.RemoveAt (0);
Destroy (topSegment);
}
}
*
*