Search Unity

Camera.position only changes after the script is recompile

Discussion in 'Scripting' started by w0ptrick, Sep 4, 2012.

  1. w0ptrick

    w0ptrick

    Joined:
    Sep 4, 2012
    Posts:
    5
    Hey guys,

    I'm currently trying to set the camera's position by the average position of a number of object positions. The average value seems to be correct, however setting the camera's position doesn't seem to change anything. The weird thing is, if I run the game, modify the script ( add a space or new line for example ) and save the script whilst the game is running, the camera's position changes properly.

    I was wondering if there was anything I may have overlooked during the game's load that might not allow me to change the camera's position?

    Code (problem occurs in the update function):

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Xml;
    6.  
    7. // managers the characters.
    8. public class CharacterManager : MonoBehaviour {
    9. // member variables.
    10.     private List<Character> m_aCharacterFact;
    11.     private bool m_bMouseDown = false;
    12.     private Vector3 m_vDirection;
    13.     private float m_fCameraZoom = 0.0f;
    14.     private Vector3 m_vAveragePosition;
    15.    
    16. // member methods.
    17.     private void ParseXML( string a_strFilename )
    18.     {
    19.         // make sure we create our array.
    20.         m_aCharacterFact = new List<Character>();
    21.        
    22.         // parse xml.
    23.         XmlDocument xmlDoc= new XmlDocument(); // create an xml document object.
    24.         xmlDoc.Load( a_strFilename );
    25.        
    26.         XmlNodeList characters = xmlDoc.GetElementsByTagName("character");
    27.         for (int i = 0; i < characters.Count; ++i)
    28.         {
    29.             GameObject tempobject = Instantiate( Resources.Load("Character") ) as GameObject;
    30.             Character temp = tempobject.GetComponent<Character>();
    31.            
    32.             float scale = Random.Range(1.0f, 0.7f);
    33.             temp.init( float.Parse( characters[i].Attributes["x"].Value ), float.Parse( characters[i].Attributes["y"].Value ), scale );
    34.            
    35.             // add pattern.
    36.             m_aCharacterFact.Add( temp );
    37.         }
    38.     }
    39.    
    40.     // Use this for initialization
    41.     void Start ()
    42.     {
    43.         m_bMouseDown = false;
    44.         m_vDirection = Vector3.zero;
    45.         m_vAveragePosition = Vector3.zero;
    46.         Vector3 cameraTransform = Camera.mainCamera.transform.position;
    47.         m_fCameraZoom = cameraTransform.z;
    48.         ParseXML( "Assets/data/spawndata.xml" );
    49.     }
    50.    
    51.     // Update is called once per frame
    52.     void Update ()
    53.     {  
    54.         if ( Input.GetKeyDown(KeyCode.A) )
    55.             m_vDirection.x += 1.0f;
    56.        
    57.         if ( Input.GetKeyUp(KeyCode.A) )
    58.             m_vDirection.x -= 1.0f;
    59.        
    60.         if ( Input.GetKeyDown(KeyCode.D) )
    61.             m_vDirection.x -= 1.0f;
    62.        
    63.         if ( Input.GetKeyUp(KeyCode.D) )
    64.             m_vDirection.x += 1.0f;
    65.    
    66.         // handles character movement.
    67.         m_vAveragePosition = Vector3.zero;
    68.         foreach ( Character temp in m_aCharacterFact )
    69.         {
    70.             temp.transform.position += m_vDirection * Time.deltaTime;
    71.             m_vAveragePosition += temp.transform.position;
    72.         }
    73.        
    74.         m_vAveragePosition /= m_aCharacterFact.Capacity;
    75.         m_vAveragePosition.z = m_fCameraZoom;
    76.        
    77.         Camera.mainCamera.transform.position = m_vAveragePosition;
    78.     }
    79.    
    80.     void LateUpdate()
    81.     {
    82.         print (Camera.mainCamera.transform.position);
    83.     }
    84. }
    85.  
    86.  
    Thanks,
     
  2. PAEvenson

    PAEvenson

    Joined:
    Nov 12, 2009
    Posts:
    47
    First thing I would check is to make sure you only have one camera in the scene. You also realize your code moves all the objects in the same direction at the same speed including the camera. Right?
     
  3. w0ptrick

    w0ptrick

    Joined:
    Sep 4, 2012
    Posts:
    5
    Hey mate,

    Yup I've only got the MainCamera in the scene. And yes, the script moves the objects based on input, averages their positions, then tries to set the camera's position within the same update function.

    The math seems to be fine as it's returning the right values.
    print(Camera.mainCamera.transform.position) is returning the right value as well, however the view doesn't seem to be updated accordingly.

    Thanks,