- Home /
Update not being called
For some reason, the update function doesn't seem to be called. I added a few debug.logs to see whether or not certain functions are called but the only one that seems to be is the Awake () function. I'm a total nub to unity so I'd need a kinda thorough explanation. This is a script used for queueing movements for my RTS so if you have the time, can you point me to or tell me if I'm on the right track? Thanks and cheers :)
using UnityEngine;
using System.Collections;
public class moveOnMouseClick : MonoBehaviour
{
private Transform myTransform; // this myTransform
private Vector3 destinationPosition1; // The destination Point
private Vector3 destinationPosition2;
private Vector3 destinationPosition3;
private Vector3 destinationPosition4;
private Vector3 destinationPosition5;
private Vector3 destinationPosition6;
private float destinationDistance1; // The distance between myTransform and destinationPosition1
private float destinationDistance2;
private float destinationDistance3;
private float destinationDistance4;
private float destinationDistance5;
private float destinationDistance6;
public float Speed;
private float moveSpeed; // The Speed the character will move
private int queue;
private int queueMove;
private Vector3 DestinationPosition;
Quaternion targetRotation1;
Quaternion targetRotation2;
Quaternion targetRotation3;
Quaternion targetRotation4;
Quaternion targetRotation5;
Quaternion targetRotation6;
private int asdf;
private bool start;
void Update ()
{
asdf = asdf;
Debug.Log ("asdf");
myTransform = transform;
if (queue == 1) {
Queue1 ();
}
if (queue == 2) {
Queue2 ();
}
if (queueMove == 1) {
if (destinationPosition1 != myTransform.position) {
Move1 ();
}
}
if (queueMove == 2) {
if (destinationPosition2 != myTransform.position) {
Move2 ();
}
}
if (queueMove == 3) {
Move3 ();
}
if (queueMove == 4) {
Move4 ();
}
if (queueMove == 5) {
Move5 ();
}
if (queueMove == 6) {
Move6 ();
}
if (queueMove == 7) {
Requeue ();
}
if (Input.GetKey (KeyCode.S)) {
Requeue ();
Stop ();
}
}
void Awake ()
{
Debug.Log ("awoken");
asdf = 5;
Requeue ();
moveSpeed = Speed;
}
//Stop command overides everything
private void Requeue ()
{
destinationPosition1 = myTransform.position;
destinationPosition2 = myTransform.position;
destinationPosition3 = myTransform.position;
destinationPosition4 = myTransform.position;
destinationPosition5 = myTransform.position;
destinationPosition6 = myTransform.position;
myTransform = transform;
queue = 1;
queueMove = 1;
}
private void Stop ()
{
myTransform.position = Vector3.MoveTowards (myTransform.position, myTransform.position, moveSpeed * Time.deltaTime);
}
void Queue1 ()
{
Debug.Log ("Queue1 is working");
Requeue ();
if (gameObject.GetComponent<Unit> ().selected && Input.GetMouseButtonDown (1)) {
queue = 1;
moveSpeed = Speed;
queue += 1;
Plane playerPlane = new Plane (Vector3.up, myTransform.position);
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast (ray, out hitdist)) {
destinationPosition1 = ray.GetPoint (hitdist);
}
targetRotation1 = Quaternion.LookRotation (destinationPosition1 - myTransform.position);
destinationDistance1 = Vector3.Distance (destinationPosition1, myTransform.position);
}
}
private void Queue2 ()
{
if (queue == 2 && Input.GetMouseButtonDown (1) && gameObject.GetComponent<Unit> ().selected && Input.GetKey (KeyCode.LeftShift)) {
moveSpeed = Speed;
queue += 1;
Plane playerPlane = new Plane (Vector3.up, myTransform.position);
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast (ray, out hitdist)) {
destinationPosition2 = ray.GetPoint (hitdist);
}
}
targetRotation2 = Quaternion.LookRotation (destinationPosition2 - myTransform.position);
destinationDistance2 = Vector3.Distance (destinationPosition2, myTransform.position);
}
private void Queue3 ()
{
if (queue == 3 && Input.GetMouseButtonDown (1) && gameObject.GetComponent<Unit> ().selected && Input.GetKey (KeyCode.LeftShift)) {
moveSpeed = Speed;
queue += 1;
Plane playerPlane = new Plane (Vector3.up, myTransform.position);
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast (ray, out hitdist)) {
destinationPosition3 = ray.GetPoint (hitdist);
}
targetRotation3 = Quaternion.LookRotation (destinationPosition3 - myTransform.position);
destinationDistance3 = Vector3.Distance (destinationPosition3, myTransform.position);
}
}
private void Move1 ()
{
myTransform.position = Vector3.MoveTowards (myTransform.position, destinationPosition1, moveSpeed * Time.deltaTime);
myTransform.rotation = targetRotation1;
if (destinationDistance1 > 1f) {
myTransform.position = Vector3.MoveTowards (myTransform.position, destinationPosition1, moveSpeed * Time.deltaTime);
} else {
moveSpeed = 0;
}
if (destinationDistance1 <= 1.5f && queue == 3) {
queueMove += 1;
}
if (destinationDistance1 <= 1.5f && queue == 2) {
Requeue ();
}
}
private void Move2 ()
{// keep track of the distance between this gameObject and destinationPosition1
// To prevent code from running if not needed
Queue2 ();
myTransform.rotation = targetRotation2;
if (destinationDistance2 > 1f) {
myTransform.position = Vector3.MoveTowards (myTransform.position, destinationPosition2, moveSpeed * Time.deltaTime);
} else {
moveSpeed = 0;
}
if (destinationDistance2 <= 1.5f && queue == 4) {
queueMove += 1;
}
}
private void Move3 ()
{
Queue3 ();
// To prevent code from running if not needed
myTransform.rotation = targetRotation3;
if (destinationDistance3 > 1f) {
myTransform.position = Vector3.MoveTowards (myTransform.position, destinationPosition3, moveSpeed * Time.deltaTime);
} else {
moveSpeed = 0;
}
if (destinationDistance3 <= 1.5f && queue == 5) {
queueMove += 1;
}
}
private void Move4 ()
{
// To prevent code from running if not needed
if (destinationDistance4 > 1.5f) {
myTransform.position = Vector3.MoveTowards (myTransform.position, destinationPosition4, moveSpeed * Time.deltaTime);
} else {
myTransform.position = Vector3.MoveTowards (myTransform.position, myTransform.position, moveSpeed * Time.deltaTime);
if (queue == 5 && Input.GetMouseButtonDown (1) && !Input.GetKey (KeyCode.LeftShift)) {
Requeue ();
}
if (queue == 5) {
queueMove += 1;
}
}
}
private void Move5 ()
{// keep track of the distance between this gameObject and destinationPosition1
// To prevent code from running if not needed
if (destinationDistance5 > 1.5f) {
myTransform.position = Vector3.MoveTowards (myTransform.position, destinationPosition1, moveSpeed * Time.deltaTime);
} else {
myTransform.position = Vector3.MoveTowards (myTransform.position, myTransform.position, moveSpeed * Time.deltaTime);
if (queue == 6 && Input.GetMouseButtonDown (1) && !Input.GetKey (KeyCode.LeftShift)) {
Requeue ();
}
if (queue == 6) {
queueMove += 1;
}
}
}
private void Move6 ()
{
// To prevent code from running if not needed
if (destinationDistance6 > 1.5f) {
myTransform.position = Vector3.MoveTowards (myTransform.position, destinationPosition1, moveSpeed * Time.deltaTime);
} else {
myTransform.position = Vector3.MoveTowards (myTransform.position, myTransform.position, moveSpeed * Time.deltaTime);
if (queue == 7 && Input.GetMouseButtonDown (1) && !Input.GetKey (KeyCode.LeftShift)) {
Requeue ();
}
if (queue == 7) {
if (queue == 7) {
queueMove += 1;
}
}
}
}
}
Are you sure the object this script is attached to is enabled?
C# has a Queue class - since yours is lowercase I don't know that it matters, but for grins maybe change that to queuex
I attached this script to the object and checked the enable box (or something). It's a simple capsule with a texture and 3d rigidbody.
Answer by supernat · Feb 10, 2014 at 06:46 AM
myTransform is not initialized, and you are trying to use it in the Requeue() method which you call from Awake. The script is crashing and being ignored from then on. You should see an Error printed in your Console window in Unity. It's best to keep the console always visible so you catch those right away (I think it prints out in red even).
Most types in C# are complex. There's only a few native types that you can instantiate in this way:
int x;
float y;
Those are initialized to 0. However, a complex type, like Transform, is a class, and you haven't created it in your code yet. For whatever reason, Unity doesn't allow you to create your own Transform, so it needs to reference an existing one. You need to do this:
private Transform myTransform;
void Awake() {
myTransform = transform;
}
The console tells me "`UnityEngine.Transform.Transform()' is inaccessible due to its protection level". Please explain further because I'm a complete nub:c
It means you can't create your own transform instance, which I've never done before. :) Unity has "protected" the default transform constructor to prevent you from making your own. It probably has some references internally to a game object, so when you create one, those refs are not valid, and thus the don't allow you to create it. I updated the answer. Basically, you really needed to assign the transform to something anyway. I assume assigning it to the local transform. Note, this doesn't really do anything for you. If you want to read/write the game object's transform, just use transform. There's too much code posted for me to look at to see how you're using myTransform at the moment, but if you explain your purpose, I can help out.
Sorry for not responding sooner; I did what you said and it worked really well! Thanks a lot:D
Your answer
![](https://koobas.hobune.stream/wayback/20220613133047im_/https://answers.unity.com/themes/thub/images/avi.jpg)