Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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.

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

Non-Static Member 1 Answer

assign a new value to a variable 0 Answers

Changing a Variable from another script. JavaScript 0 Answers

Game Objects not using same script independently. 1 Answer

Scene stores variables after scene reload 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges