Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by stevensu1838 · Jun 02, 2019 at 03:39 PM · variablevariablesaccessing from any scriptaccessing scripts

Help! Can't not access the variable linearVelocity of TwistPublisher.cs from another script in Unity

Hi all, My OS is: Windows 10 My Unity Version is: Unity 2018.4 My ROS Distribution is: Kinetic My ROS # Version is:1.4 TwistPublisher.cs is just a sample script from ROSsharp unity package which help you read the velocity of a unity gameobject. The content of TwistPublisher.cs is shown below:

 using UnityEngine;
 
 namespace RosSharp.RosBridgeClient
 {
     public class TwistPublisher : Publisher<Messages.Geometry.Twist>
     {
         public Transform PublishedTransform;
 
         private Messages.Geometry.Twist message;
         private float previousRealTime;        
         private Vector3 previousPosition = Vector3.zero;
         private Quaternion previousRotation = Quaternion.identity;
 
         protected override void Start()
         {
             base.Start();
             InitializeMessage();
         }
 
         private void FixedUpdate()
         {
             UpdateMessage();
         }
 
         private void InitializeMessage()
         {
             message = new Messages.Geometry.Twist();
             message.linear = new Messages.Geometry.Vector3();
             message.angular = new Messages.Geometry.Vector3();
         }
         private void UpdateMessage()
         {
             float deltaTime = Time.realtimeSinceStartup - previousRealTime;
 
             Vector3 linearVelocity = (PublishedTransform.position - previousPosition)/deltaTime;
             Vector3 angularVelocity = (PublishedTransform.rotation.eulerAngles - previousRotation.eulerAngles)/deltaTime;
                 
             message.linear = GetGeometryVector3(linearVelocity.Unity2Ros()); ;
             message.angular = GetGeometryVector3(- angularVelocity.Unity2Ros());
 
             previousRealTime = Time.realtimeSinceStartup;
             previousPosition = PublishedTransform.position;
             previousRotation = PublishedTransform.rotation;
 
             Publish(message);
         }
 
         private static Messages.Geometry.Vector3 GetGeometryVector3(Vector3 vector3)
         {
             Messages.Geometry.Vector3 geometryVector3 = new Messages.Geometry.Vector3();
             geometryVector3.x = vector3.x;
             geometryVector3.y = vector3.y;
             geometryVector3.z = vector3.z;
             return geometryVector3;
         }
     }
 }


I am trying to read the variable linearVelocity of TwistPublisher.cs from another script which is attached on a normal 3D gameobject in Unity. As you know, linearVelocity of TwistPublisher.cs can represent the velocity of a related gameobject in unity(In my case, the related gameobject is a cube), I simply want to access this linearVelocity from another script which I named CsvWriteScript. Please take a look at my CsvWriteScript:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Text;
 using System.IO;
 using System;
 using RosSharp.RosBridgeClient;
 
 public class CsvWriteScript : MonoBehaviour
 {
     GameObject obj;
     TwistPublisher twistpublisherScript;
 
     void Start()
     {  
         obj = GameObject.FindGameObjectWithTag("RosConnector");
         twistpublisherScript = obj.GetComponent<TwistPublisher>();
     }
 
     void Update()
     {
         UnityEngine.Debug.Log("the velocity of this cube:" + twistpublisherScript.linearVelocity);
     }
 }

In the TwistPublisher.cs I used Debug.Log("Vlinear" + linearVelocity); to print the velocity

 void UpdateMessage()
         {
             float deltaTime = Time.realtimeSinceStartup - previousRealTime;
             Vector3 linearVelocity = (PublishedTransform.position - previousPosition) / deltaTime;
             Vector3 angularVelocity = (PublishedTransform.rotation.eulerAngles - previousRotation.eulerAngles)/deltaTime; 
             //Steven
             Debug.Log("Vlinear" + linearVelocity);

Then I moved the cube in unity by dragging it with the mouse, it turned out the print from Debug.Log("Vlinear" + linearVelocity); was updating but the print from UnityEngine.Debug.Log("the velocity of this cube:" + twistpublisherScript.linearVelocity); was always(0, 0 , 0)

I am sure the link between the TwistPublisher.cs and my CsvWriteScript is built. Any chance you know why the twistpublisherScript.linearVelocity does't update when the linearVelocity from your TwistPublisher.cs is changing?

Thanks a million for your help.

To find out the problems, I write my own script to read the velocity of the cube object to replace the TwistPublisher.cs. The script is shown below

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ObjVelocityReader : MonoBehaviour
 {
     Vector3 PrevPos;
     Vector3 NewPos;
     Vector3 PrevRot;
     Vector3 NewRot;
 
     public Vector3 ObjVelocity;
     public Vector3 ObjRotation;
     // Use this for initialization
     void Start()
     {
         PrevPos = transform.position;
         PrevRot = transform.eulerAngles;
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         transform.rotation = Quaternion.identity;
 
         NewPos = transform.position;  // each frame track the new position
         ObjVelocity = (NewPos - PrevPos) / Time.fixedDeltaTime;
         PrevPos = NewPos;  // update position for next frame calculation
 
         NewRot = transform.eulerAngles;  // each frame track the new rotation
         ObjRotation = (NewRot - PrevRot) / Time.fixedDeltaTime;
         PrevRot = NewRot;  // update position for next frame calculation
     }
 }
       


And then I used my CsvWriteScript(shown below)to access the velocity variable from my ObjVelocityReader(it is a replacement to your TwistPublisher.cs here), it was a success. I could access the velocity from my CsvWriteScript successfully. So I really couldn't figure out how I should modify the TwistPublisher.cs to fix the problem. Can you please do me a favor? Really appreciated.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Text;
 using System.IO;
 using System;
 using RosSharp.RosBridgeClient;
 
 public class CsvWriteScript : MonoBehaviour
 {
     GameObject obj;
     ObjVelocityReader velocityScript;
 
     // Start is called before the first frame update
     void Start()
     {
         //obj = GameObject.FindGameObjectWithTag("RosConnector");
         //twistpublisherScript = obj.GetComponent<TwistPublisher>();
         obj = GameObject.Find("Sphere");
         velocityScript = obj.GetComponent<ObjVelocityReader>();        
     }
     void Update()
     {
         UnityEngine.Debug.Log("V:" + velocityScript. ObjVelocity);
     }
 }





Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by stevensu1838 · Jun 03, 2019 at 01:31 AM

Problem solved by removing Vector3 in front for linearVelcity in TwistPubliser.cs. Not sure why.

Vector3 linearVelocity = (transform.position - previousPosition)/deltaTime; Vector3 angularVelocity = (transform.rotation.eulerAngles -previousRotation.eulerAngles)/deltaTime; Now it is:

namespace RosSharp.RosBridgeClient { public class TwistProvider : MessageProvider { public Vector3 linearVelocity;

     private void UpdateMessage()
     {
     Vector3 angularVelocity = linearVelocity = (transform.position - previousPosition)/deltaTime;
      }

@stevensu1838

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

115 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

Related Questions

How to get damage variable from another script ? 1 Answer

How to increase score for objects that form larger geometries? 0 Answers

Custom Editor Access to Scene Objects Variables (in the same way as UnityEvents) 1 Answer

What makes a prefab alter their variables at runtime? 2 Answers

C# taking variable from other script 2 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