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

Help with a script

Discussion in 'Scripting' started by The Odd Fox, Mar 19, 2016.

  1. The Odd Fox

    The Odd Fox

    Joined:
    Mar 19, 2016
    Posts:
    3
    I'm getting an error with this script and I can't figure out the problem.
    Errror: Assets/Fixer.cs(13,43): error CS1525: Unexpected symbol `Hexagon', expecting `)', `,', `;', `[', or `='

    Code:
    using UnityEngine;
    using System.Collections;

    public class Fixer : MonoBehaviour {

    public GameObject Hexagon;

    // Update is called once per frame
    void Update () {

    Transform hexaTrans Hexagon.GetComponent<Transform>();
    hexaTrans.rotation = new Vector3 (0, 0, 0);
    }
    }
     
  2. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    use this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Fixer : MonoBehaviour {
    5.  
    6. public GameObject Hexagon;
    7.  
    8. // Update is called once per frame
    9. void Update () {
    10.  
    11. Transform hexaTrans = Hexagon.GetComponent<Transform>();
    12. hexaTrans.rotation = new Vector3 (0, 0, 0);
    13. }
    14. }
    15.  
    also learn code tags :)
     
  3. The Odd Fox

    The Odd Fox

    Joined:
    Mar 19, 2016
    Posts:
    3
    I slightly changed yours to fix a bug, here it is now...
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Fixer : MonoBehaviour {
    6.    
    7.     public GameObject Hexagon;
    8.    
    9.     // Update is called once per frame
    10.     void Update () {
    11.        
    12.         Transform hexaTrans = Hexagon.GetComponent<Transform>();
    13.         hexaTrans.rotation = Quaternion.Euler (0, 0, 0);
    14.     }
    15. }
    16.  
    My only other question is am I able to take the Z rotation out of 'Quaternion.Euler (0, 0, 0);'.
    So that then it only freezes the X and Y?
     
  4. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    No, you need all the values, otherwise unity does not know if you want rotations in the z-axis and will give you errors.
     
  5. Austin-Gregory

    Austin-Gregory

    Joined:
    Sep 19, 2013
    Posts:
    78
    If you're wanting the X and Y to be frozen and you want to rotate on the Z, just pass your Quaternion.Euler a variable that is updated with your desired Z rotation, no? Then just reference this variable when you're changing the Z rotation. That is if you must go about it this way.
     
  6. The Odd Fox

    The Odd Fox

    Joined:
    Mar 19, 2016
    Posts:
    3
    Is this what you meant?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Fixer : MonoBehaviour {
    6.    
    7.     public GameObject Hexagon;
    8.     public float zrep;
    9.  
    10.     // Update is called once per frame
    11.     void Update () {
    12.  
    13.  
    14.         Transform hexaTrans = Hexagon.GetComponent<Transform>();
    15.         float zrep = hexaTrans.rotation.z;
    16.         hexaTrans.rotation = Quaternion.Euler (0, 0, zrep);
    17.     }
    18. }
    19.  
     
  7. Austin-Gregory

    Austin-Gregory

    Joined:
    Sep 19, 2013
    Posts:
    78
    Well, no. You're setting it every frame to be the rotation of hexaTrans. Each frame, if it sets it to be that, it will always be that. With your example there's no reason to have the zrep field at all since you're just passing the current rotation to it's current rotation.

    If you wanted zrep to control the z rotation, just assign it to the current rotation in Start instead of every frame.