Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Position is modified into Update() but not into other functions

Discussion in 'Scripting' started by Desmazio, May 7, 2020.

  1. Desmazio

    Desmazio

    Joined:
    Mar 3, 2020
    Posts:
    13
    Hello, this is a part of my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.  
    8.     void Update()
    9.     {
    10.         print("Update" + transform.position);
    11.     }
    12.  
    13.     public Vector3 SendPosition()
    14.     {
    15.         print("Function" + transform.position);
    16.         return transform.position;
    17.     }
    18. }
    the problem appear when I execute: in the console, while I move the object i can see the two outputs. The "Update" one works fine, but the "Function" one prints only the initial position, and when it's called by another script, the output remain the same.
    Thanks for the help.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Can you show the other script where you are calling the function?
     
  3. Nyfirex

    Nyfirex

    Joined:
    Jun 26, 2019
    Posts:
    179
    We need to know other scripts too and your objective because i didn't understand what you need. Do you need to get the position of the script from other scripts or you need to send the position from where you call the function?
     
  4. Desmazio

    Desmazio

    Joined:
    Mar 3, 2020
    Posts:
    13
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Pisces : MonoBehaviour
    6. {
    7.     [SerializeField] GameObject player_prefab;
    8.     [SerializeField] float laser_reload = 5f;
    9.     bool is_alive = true;
    10.  
    11.     Vector3 player_pos;
    12.     Vector3 direction;
    13.     float rotation_z;
    14.     Coroutine laser_coroutine;
    15.  
    16.     void Start()
    17.     {
    18.        
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         if (is_alive)
    24.         {
    25.             rotation_z = GetPlayerAngulation();
    26.         }
    27.     }
    28.  
    29.     private float GetPlayerAngulation()
    30.     {
    31.         player_pos = player_prefab.GetComponent<Player>().SendPosition();
    32.  
    33.         direction = player_pos - transform.position;
    34.         rotation_z = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
    35.         return rotation_z;
    36.     }
    37.  
    38.     IEnumerator FireLaser()
    39.     {
    40.         while (true)
    41.         {
    42.             shark_eye.GetComponent<SharkEye>().Fire(rotation_z);
    43.             yield return new WaitForSeconds(laser_reload);
    44.         }
    45.    }
    46.  
    47. }
     
  5. Desmazio

    Desmazio

    Joined:
    Mar 3, 2020
    Posts:
    13
    Ok I managed to get it working by myself. Instead of call a function between the two scripts, it's better to use the function GameObject.Find("Player").transform.position;