• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by moreelen · Jan 02, 2017 at 03:08 PM · javascriptvariablesstaticnon-static

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:

alt text

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.

alt text

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.

untitled-2.png (185.7 kB)
15878440-10154000913202035-59620455-o.png (364.2 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image moreelen · Jan 02, 2017 at 02:41 PM 0
Share

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();
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

119 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Static Class Strings not truly null? 1 Answer

Parameter for animation not updating 0 Answers

Static Array JS 0 Answers

How do I access a variable/class from a C# file in a JS file? 1 Answer

assign a new value to a variable 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges