- Home /
How can I get the Transform from an Object (target of UnityEvent)?
Hello!
I'm trying to draw a gizmo line from the current object to the target object of each of its UnityEvents. I've been able to figure out that the target of a UnityEvent is an 'object' (not a GameObject).
How can I get a reference to the Transform component of these objects?
I'm able to get a reference to the object correctly, and I can confirm this with Debug Logs. I suspect I need to do something using GetType, but I've been unable to figure out what, or how I should write my code.
IEnumerator DrawHelperLines () {
Gizmos.color = Color.yellow;
int i = 0;
foreach (ScriptedSequenceEvent currentEvent in events) {
object obj = currentEvent.scriptedEvent.GetPersistentTarget (0);
// ~~~ What goes here??? ~~~
Transform target;
Gizmos.DrawLine (transform.position, target.position);
i++;
}
yield break;
}
Thanks in advance :)
Answer by bobisgod234 · May 05, 2017 at 01:53 AM
GameObject gameObject = obj as GameObject;
Transform target = gameObject.transform;
Perfect, thank you! Didn't realise it was so simple.
I was able to simplify it even more using your code as a starting point, as I don't need to do anything with the Transform or Object.
Here's my final code:
IEnumerator DrawHelperLines () {
Gizmos.color = Color.yellow;
int i = 0;
foreach (ScriptedSequenceEvent currentEvent in events) {
GameObject go = currentEvent.scriptedEvent.GetPersistentTarget (0) as GameObject;
Gizmos.DrawLine (transform.position, go.transform.position);
i++;
}
yield break;
}
Your answer
Follow this Question
Related Questions
nut and bolt object construction system? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Best way to defining scripts within a list inside the editor? 1 Answer
Object Pooling 1 Answer