Why is my script only running when object has been selected beforehand?
This piece of Javascript is giving me troubles.
var originalPosition : Vector3;
var osHeight = 0.05;
var delay = 0;
function Start () {
// when the object starts, we record its initial position
originalPosition = transform.position;
}
function Update () {
// Set the x position to loop between values osHeight and a set delay.
transform.position.y = originalPosition.y + Mathf.Sin(Time.time + delay) * osHeight;
Debug.Log("YPosition = " + transform.position.y);
}
All seems fine but only runs on play when the object is selected like this:
Otherwise it doesn't run.
I've tried the debugger and I've noticed that the y position does oscilate. However the value on the object and of YPosition is different.
Why is this happening and how do I fix this?
Sorry if it's a really simple question. I've tried googling answers but can't seem to fix it.
I have one other script attached to these which might be affecting it. Useful?
using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
#endif
[ExecuteInEdit$$anonymous$$ode]
public class ParallaxObject : $$anonymous$$onoBehaviour
{
public Camera connectedCamera;
public float xSpeed = 0;
//public float ySpeed = 0;
public float cameraSizeRatio = 1;
public bool resetPosition;
public bool updatePosition = false;
public Vector3 centerPosition;
public Vector3 cameraCenterPosition;
void Start ()
{
if (connectedCamera == null)
connectedCamera = Camera.main;
if (centerPosition == Vector3.zero && cameraCenterPosition == Vector3.zero)
GetNewParallaxPosition();
}
void GetNewParallaxPosition()
{
centerPosition = transform.position;
cameraCenterPosition = connectedCamera.transform.position;
}
void UpdatePosition()
{
Vector3 _newPos = Vector3.zero;
Vector3 _camPos = connectedCamera.transform.position;
float _divideByScale = $$anonymous$$athf.Log(connectedCamera.orthographicSize);
_divideByScale = $$anonymous$$athf.Log10(connectedCamera.orthographicSize);
_divideByScale = $$anonymous$$athf.Log(connectedCamera.orthographicSize, 2);
_newPos.x = centerPosition.x + ((_camPos.x - cameraCenterPosition.x) * (xSpeed / _divideByScale ));
_newPos.y = centerPosition.y;// + ((_camPos.y - cameraCenterPosition.y) * (ySpeed / _divideByScale));
_newPos.z = centerPosition.z;
transform.position = _newPos;
updatePosition = false;
}
void LateUpdate ()
{
#if UNITY_EDITOR
if (resetPosition)
{
if(Selection.activeGameObject != gameObject)
resetPosition = false;
GetNewParallaxPosition();
}
if (Selection.activeTransform == transform)
return;
#endif
if (Application.isPlaying || updatePosition)
UpdatePosition();
}
}
Answer by hexagonius · Jan 03, 2017 at 08:40 PM
you can't change transform.position.y directly. you need to save the current position as Vector3, alter y and write it back to position.
Your answer
Follow this Question
Related Questions
Non-Static Member 1 Answer
assign a new value to a variable 0 Answers
Changing a Variable from another script. JavaScript 0 Answers