Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by jpthek9 · Feb 10, 2014 at 04:39 AM · updatefunctionrtsqueueclicktomove

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;
                                 }
                         }
 
                 
                 }
         }
 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Feb 10, 2014 at 04:43 AM 1
Share

Are you sure the object this script is attached to is enabled?

avatar image getyour411 · Feb 10, 2014 at 04:46 AM 0
Share

C# has a Queue class - since yours is lowercase I don't know that it matters, but for grins maybe change that to queuex

avatar image jpthek9 · Feb 10, 2014 at 04:46 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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;
   }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jpthek9 · Feb 10, 2014 at 02:41 PM 0
Share

The console tells me "`UnityEngine.Transform.Transform()' is inaccessible due to its protection level". Please explain further because I'm a complete nub:c

avatar image supernat · Feb 10, 2014 at 07:50 PM 0
Share

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.

avatar image jpthek9 · Feb 27, 2014 at 01:06 AM 0
Share

Sorry for not responding sooner; I did what you said and it worked really well! Thanks a lot:D

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Question... Use the switch to update or function 0 Answers

Why doesen't the function get called the second itme? 1 Answer

checking multiple textures at once using functio update 1 Answer

Performance Optimization ~Function Update: Loop or Once ? 5 Answers

Update() doesn't update so well 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges