Search Unity

Prolem Transporting an Object?

Discussion in 'Scripting' started by busterlock, Aug 29, 2016.

  1. busterlock

    busterlock

    Joined:
    Sep 26, 2015
    Posts:
    47
    Hello, everybody!
    This is my issue: I have a script that moves NPCs along different environments in pre-established locations. The way I do this is by generating a random number, and assigning each number to the location.
    However, I have another script that counts how many days have passed since the game has started, and it has a very simple logic: each action the user takes, a counter goes up, and when the counter reaches 3, a day passes. and by the end of each day, a new location is assigned. Here is the script for the transporting:
    Code (CSharp):
    1.  
    2.  
    3. //Create 5 random locations where every character will be translated
    4.  
    5.     public float number;
    6.     public Transform pos1;
    7.     public Transform pos2;
    8.     public Transform pos3;
    9.     public Transform pos4;
    10.     public Transform pos5;
    11.     public float newCount;
    12.  
    13.     void Start()
    14.     {
    15.         number = Random.Range(1, 5);
    16.         GameObject gameClockObject = GameObject.Find("GameClockObject");
    17.         GameClock gameClock = gameClockObject.GetComponent<GameClock>();
    18.         newCount = gameClock.dayCount;
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         if (number == 1)
    24.         {
    25.             transform.position = pos1.transform.position;
    26.             Debug.Log("Position 1");
    27.         }
    28.         if (number == 2)
    29.         {
    30.             transform.position = pos2.transform.position;
    31.             Debug.Log("Position 2");
    32.         }
    33.         if (number == 3)
    34.         {
    35.             transform.position = pos3.transform.position;
    36.             Debug.Log("Position 3");
    37.         }
    38.         if (number == 4)
    39.         {
    40.             transform.position = pos4.transform.position;
    41.             Debug.Log("Position 4");
    42.         }
    43.         if (number == 5)
    44.         {
    45.             transform.position = pos5.transform.position;
    46.             Debug.Log("Position 5");
    47.         }
    48.         GameObject gameClockObject = GameObject.Find("GameClockObject");
    49.         GameClock gameClock = gameClockObject.GetComponent<GameClock>();
    50.         if (newCount != gameClock.dayCount)
    51.         {
    52.             number = Random.Range(1, 5);
    53.             newCount = gameClock.dayCount;
    54.             Debug.Log("It changed");
    55.         }
    56.     }
    57. }
    58.  
    I use the "newCount" variable, to compare to the previous value of the dayCount variable, and each time it changes, the location of the NPCs changes too.
    The problem is that inside the Console, I keep getting this:
    (Check the attached file)
    (The 276 messages on the right side of the screen, that tells me the "if" statement is still running). This doesn't stop the game from working, but please notice that that number is only 276 because I paused the game, If I had kept running it, the number would increase. I'm afraid this might create an optimization problem, and somewhere in my mind I know that the solution is very simple, but I just can't think of it.
    Could you guys help, please?
     

    Attached Files:

    • yes.png
      yes.png
      File size:
      15.3 KB
      Views:
      197
  2. LTK

    LTK

    Joined:
    Jul 16, 2015
    Posts:
    22
    You can use event when you change it to change location :D, don't in Update() method.
     
  3. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Just a quick lesson (refresher) on the built in function of Unity:
    Start() Happens one time and one time only when the scene starts
    Update() happens every frame (most likely around 60 times a second) so this code gets called a lot. Thats why any Debugs in there will just spam your screen.

    A few tips:
    1. If you have the same exact code in 2 differnt places you might want to consider making a function (for example your code that changes newCount) This makes it more readable and less bug prone if you change it.(you won't forget to change all the copies of it since there is only one).
    2. If it all possible avoid using GetComponent<> of any kind in Update. They are relatively expensive (they search through all the game objets to find the exact one you want.) and you don't want to keep calling this over and over 60 times a second.
    Here's how I would handle your code:
    Code (CSharp):
    1.     //Create 5 random locations where every character will be translated
    2.  
    3.     public float number;
    4.     public Transform pos1;
    5.     public Transform pos2;
    6.     public Transform pos3;
    7.     public Transform pos4;
    8.     public Transform pos5;
    9.  
    10.     // this doesn't need to be set in the editor so private
    11.     private float newCount;
    12.     private int positionNumber; // this was your variable called Number i just made it more descriptive
    13.     // we need to decare these globally so all the functions can access them
    14.     private GameObject gameClockObject;
    15.     private GameClock gameClock;
    16.  
    17.     void Start()
    18.     {
    19.         // Get the handles for the ClockObject and GameClock.  We only need to do this one time
    20.         // since our variables are class variables they will "remember" what they were set to.
    21.         gameClockObject = GameObject.Find("GameClockObject");
    22.         gameClock = gameClockObject.GetComponent<GameClock>();
    23.         UpdateClockAndNumber();
    24.         ResetPositions();
    25.      
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         // all we want to do each update is check if the day has incremented
    31.         // if it has then we'll call our other functions
    32.         if (newCount != gameClockObject.dayCount)
    33.         {
    34.             UpdateClockAndNumber();
    35.             ResetPositions()
    36.          }
    37.     }
    38.  
    39.     void ResetPositions()
    40.     {
    41.         if (positionNumber == 1)
    42.         {
    43.             transform.position = pos1.transform.position;
    44.             Debug.Log("Position 1");
    45.         }
    46.         if (positionNumber == 2)
    47.         {
    48.             transform.position = pos2.transform.position;
    49.             Debug.Log("Position 2");
    50.         }
    51.         if (positionNumber == 3)
    52.         {
    53.             transform.position = pos3.transform.position;
    54.             Debug.Log("Position 3");
    55.         }
    56.         if (positionNumber == 4)
    57.         {
    58.             transform.position = pos4.transform.position;
    59.             Debug.Log("Position 4");
    60.         }
    61.         if (positionNumber == 5)
    62.         {
    63.             transform.position = pos5.transform.position;
    64.             Debug.Log("Position 5");
    65.         }
    66.     }
    67.     void UpdateClockAndNumber()
    68.     {
    69.         positionNumber = Random.Range(1, 5);
    70.         newCount = gameClock.dayCount;
    71.     }
     
  4. LTK

    LTK

    Joined:
    Jul 16, 2015
    Posts:
    22
    Code (csharp):
    1. if (newCount != gameClockObject.dayCount)
    I don't think type GameObject have a public fied with name is dayCount, check your code.
    Can you show the code GameClock class?
    I think, you have change dayCount in GameClock class, I recommend use Event C# (https://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx)
    to callback to UpdateClockAndNumber and ResetPosition when change dayCount
    I changed dev Android App, so I do not have Unity software on my computer to demo code, sorry for this
     
    Last edited: Aug 30, 2016