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. Dismiss Notice

what is wrong with my script ?

Discussion in 'Scripting' started by m-y, Jul 25, 2016.

  1. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    470
    Code (CSharp):
    1. void Start () {
    2.         PlayerPrefs.SetFloat("transformx",transform.position.x);
    3.         PlayerPrefs.SetFloat("transformz", transform.position.z);
    4.  
    5.  
    6.     }
    7.  
    8.     // Update is called once per frame
    9.     void Update() {
    10.         // print(PlayerPrefs.GetFloat(transform.position + ""));
    11.        // print(transform.position);
    12.     // print (   PlayerPrefs.GetFloat("mosaad",transform.position.x));
    13.  
    14.     }
    15.     void OnTriggerEnter (Collider Hit )
    16.     {
    17.         if (Hit.gameObject.tag== "Amira")
    18.         {
    19.             float X = Hit.gameObject.transform.position.x;
    20.             float Z = Hit.gameObject.transform.position.z;
    21.            X = PlayerPrefs.GetFloat("transformx");
    22.             Z = PlayerPrefs.GetFloat("transformz");
    23.             print("Hit Amira");
    24.         }
    25.     }
    Hello guys
    Actually i am trying to create script to Switch Two object position At the same time as you can see in the picture
    i want the man object when hit the woman object
    switch their position in the boxes
    so i tried to create the script
    to playerprefs the first position of the man and sending it to the woman when hit it
    it is printing
    but doesn't work !
    ex1.png
    Code (CSharp):
    1.  void OnTriggerEnter (Collider Hit )
    2.     {
    3.         if (Hit.gameObject.tag== "Amira")
    4.         {
    5.             float X = Hit.gameObject.transform.position.x;
    6.             float Z = Hit.gameObject.transform.position.z;
    7.            X = PlayerPrefs.GetFloat("transformx");
    8.             Z = PlayerPrefs.GetFloat("transformz");
    9.             print("Hit Amira");
    10.         }
    11.     }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ok, why are you using playerprefs for this?

    at no point are you setting the position of anything...
     
  3. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    470
    i used it
    to store the positions of object
    then send that value of position to the other object
    to make the switch position i need !
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    playerprefs is more for storing information between plays of the game. Not for passing information between two objects during a single runtime... :confused:
     
  5. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    470
    so what is the best way to passing information during runtime ? !
     
  6. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
  7. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    470
    Are you sure about using property here ! ?
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    When you do float X = ..., you're creating a new variable. It doesn't in any way link to the transform on the right hand side of = there.

    To move the hit object to the values from playerprefs, you have to assign a new Vector3 to the position:

    Code (csharp):
    1. Vector3 positionFromPrefs = new Vector3(PlayerPrefs.GetFloat("transformx"), 0f, PlayerPrefs.GetFloat("transformz"));
    2.  
    3. Hit.gameObject.transform.position = positionFromPrefs;
    You have to handle the other object in the same way, getting the position you're moving it to, and then assigning it to your transform.
     
  9. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    470
    ok thanks mate