Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Use script from one game object to affect a child in another game object?

Discussion in 'Scripting' started by vautourb, Jul 31, 2021.

  1. vautourb

    vautourb

    Joined:
    Jul 8, 2021
    Posts:
    10
    I have 2 gameObjects

    Compass
    GPSDevice




    it doesn't seem like the link for the photo so link to image: https://ibb.co/WG9q79x

    I am trying to write a script where compass takes the gps string from GPSdevice script named Serial.cs and take out the public
    Code (CSharp):
    1. static string[] GPSdata;
    and use the the orientation value
    Code (CSharp):
    1. GPSdata[9]
    and put it into Gameobject Compass RawImage UVRect x: variable.

    I have followed a few youtube tutorials and done some reading, and I am left with confusion and even less understanding of gameobjects then I had before. and now with this error:

    error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

    here is the code for GPSOrientation.cs

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UNityEngine.UI
    6.  
    7. public class GPSOrientation : MonoBehaviour
    8. {
    9. public GameObject = obj
    10.      // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         obj.transform.Compass.CompassRawImage = Serial.GPSdata[9];
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.      
    20.     }
    21.  
    22.   }
    23.  
    and the code for Serial.cs:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5. using System.IO.Ports;
    6. using TMPro;
    7.  
    8. public class Serial : MonoBehaviour
    9. {
    10.  
    11.     SerialPort stream = new SerialPort("COM5", 9600);
    12.     public GameObject GPS;
    13.     public Vector3 rot;
    14.     public Vector3 rot2;
    15.     public string receivedstring;
    16.     public static string[] GPSdata;
    17.     public string[] GPSdata_received;
    18.     public int dataLength = 12;
    19.  
    20.  
    21.  
    22.     void Start()
    23.     {
    24.         stream.Open(); //Open the Serial Stream.
    25.         Debug.Log("Port open");
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         // Create new data
    31.         GPSdata_received = new string[dataLength];
    32.  
    33.         receivedstring = stream.ReadLine(); //Read the Serial Stream
    34.         stream.BaseStream.Flush(); //Clear the serial information so we assure we get new information.
    35.  
    36.         if (receivedstring.Contains("GPRMC"))
    37.         {
    38.             string[] GPSdata = receivedstring.Split(','); //Seperate stream by "," Delimiter
    39.             for (int i = 0; i < dataLength; i++)
    40.             {
    41.                 GPSdata_received[i] = GPSdata[i];
    42.                 if (i == 2)
    43.                 {
    44.                    GetComponent<TMP_Text>().text = "Lon : " + GPSdata[4] + "  : " + GPSdata[5] + "\nLat  : " + GPSdata[6] + " : " + GPSdata[7];
    45.  
    46.                 }
    47.              
    48.             }
    49.         }
    50.    
    51.     }
    52.  
    53.  
    54.     public void OnApplicationQuit()
    55.     {
    56.  
    57.         if (receivedstring != null)
    58.         {
    59.             if (stream.IsOpen)
    60.             {
    61.                 Debug.Log("closing serial port");
    62.                 stream.Close();
    63.             }
    64.  
    65.             receivedstring = null;
    66.         }
    67.  
    68.     }
    69.  
    70. }
     
    Last edited: Jul 31, 2021
  2. mikeohc

    mikeohc

    Joined:
    Jul 1, 2020
    Posts:
    215
    What's this?
    Code (CSharp):
    1. obj.transform.Compass.CompassRawImage = Serial.GPSdata[9];
    Or did you mean
    Code (CSharp):
    1. obj.GetComponent<Compass>().CompassRawImage = Serial.GPSdata[9];
    You want to put a string as a float variable?
    According to your script, GPSdata is an array of strings. You'd need to parse string to a float.
    float x = float.Parse(Serial.GPSdata[9]);
     
  3. vautourb

    vautourb

    Joined:
    Jul 8, 2021
    Posts:
    10
    so I'll use the image I added above to explain what I am trying to do



    If I can do it from Serial.cs I will, but I want to take GPSdata[9] convert it to a double then place that result into the gameobject Compass.RawImage UV Rect x position.

    I hope this helps explain what I am trying to achieve.
     
  4. mikeohc

    mikeohc

    Joined:
    Jul 1, 2020
    Posts:
    215
    The images aren't showing for me.

    so Compass is just the name for your RawImage UI gameobject?

    Try this:
    Code (CSharp):
    1. public GameObject _compassWithRawImageComponent;
    2.  
    3.     void Start()
    4.     {
    5.         RawImage compassRawImage = _compassWithRawImageComponent.transform.GetComponent<RawImage>();
    6.         Vector2 originalSize = compassRawImage.uvRect.size;
    7.         Vector2 originalPos = compassRawImage.uvRect.position;
    8.         compassRawImage.uvRect = new Rect(Serial.GPSdata[9], originalPos.y, originalSize.x, originalSize.y);
    9.     }
    To set uvRect in RawImage component, you can create a new rect for it.
     
  5. vautourb

    vautourb

    Joined:
    Jul 8, 2021
    Posts:
    10
    Yes the RawImage is on the compass in the ui. I did receive an error on play I must have missed it

    NullReferenceException: Object reference not set to an instance of an object
    GPSOrientation.Start () (at Assets/GPSOrientation.cs:15)

    Here is the link to the image https://ibb.co/WG9q79x that way you should be able to atleast see it.


    So I made the script GPSOrientation.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class GPSOrientation : MonoBehaviour
    7. {
    8.     public GameObject _compassWithRawImageComponent;
    9.  
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         RawImage compassRawImage = _compassWithRawImageComponent.transform.GetComponent<RawImage>();
    15.         Vector2 originalSize = compassRawImage.uvRect.size;
    16.         Vector2 originalPos = compassRawImage.uvRect.position;
    17.         compassRawImage.uvRect = new Rect(float.Parse(Serial.GPSdata[9]), originalPos.y, originalSize.x, originalSize.y);
    18.  
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.    
    25.     }
    26.  
    27.   }
    28.  
    I added the code to the Compass, then in inspector, dragged the compass into the gameObject with variable v rect. I get no errors however the info does not update
     
  6. mikeohc

    mikeohc

    Joined:
    Jul 1, 2020
    Posts:
    215
    NullReferenceException just means you haven't assign anything there for the script to use it. So it's currently "null".

    As for the info not updating, it's probably because you have
    compassRawImage.uvRect = new Rect(float.Parse(Serial.GPSdata[9]), originalPos.y, originalSize.x, originalSize.y);
    in start.

    Start is only called once, and doesn't refresh the values.
     
  7. vautourb

    vautourb

    Joined:
    Jul 8, 2021
    Posts:
    10
    I moved it to update sorry brain wasn't working. I added a Debug.Log(GPSdata[9]; and it throws out 310721, so the data is there. but it does not go to the rect for transformation, could the float.Parse(Serial.GPSdata[9]) be the issue?
     
  8. mikeohc

    mikeohc

    Joined:
    Jul 1, 2020
    Posts:
    215
    what does Debug.Log(compassRawImage.uvRect.x); return?
     
  9. vautourb

    vautourb

    Joined:
    Jul 8, 2021
    Posts:
    10
    Assets\GPSOrientation.cs(25,55): error CS1503: Argument 2: cannot convert from 'float' to 'UnityEngine.Object'
     
    Last edited: Jul 31, 2021
  10. mikeohc

    mikeohc

    Joined:
    Jul 1, 2020
    Posts:
    215
    Paste the line this error brings you to.
     
  11. vautourb

    vautourb

    Joined:
    Jul 8, 2021
    Posts:
    10
    That's the
    Code (CSharp):
    1. Debug.Log("Show me ", compassRawImage.uvRect.x);
     
  12. mikeohc

    mikeohc

    Joined:
    Jul 1, 2020
    Posts:
    215
    Debug.Log("Show me " + compassRawImage.uvRect.x);
     
  13. vautourb

    vautourb

    Joined:
    Jul 8, 2021
    Posts:
    10
    I think I have been going about this all wrong
    Just wrote a new script from reading a lot more and watching a few tutorials which makes a working compass.. now i Just need to feed in the variables and see if it works.
     
  14. vautourb

    vautourb

    Joined:
    Jul 8, 2021
    Posts:
    10
    This code works, so now I just need to work with the GPS data.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.UI;
    4. using UnityEngine;
    5. public class Compass : MonoBehaviour
    6. {
    7.     public RawImage CompassImage;
    8.     public Transform Player;
    9.     public Text CompassDirectionText;
    10.  
    11.     public void Update()
    12.     {
    13.         //Get a handle on the Image's uvRect
    14.         CompassImage.uvRect = new Rect(Player.localEulerAngles.y / 360, 0, 1, 1);
    15.  
    16.         // Get a copy of your forward vector
    17.         Vector3 forward = Player.transform.forward;
    18.  
    19.         // Zero out the y component of your forward vector to only get the direction in the X,Z plane
    20.         forward.y = 0;
    21.  
    22.         //Clamp our angles to only 5 degree increments
    23.         float headingAngle = Quaternion.LookRotation(forward).eulerAngles.y;
    24.         headingAngle = 5 * (Mathf.RoundToInt(headingAngle / 5.0f));
    25.  
    26.         //Convert float to int for switch
    27.         int displayangle;
    28.         displayangle = Mathf.RoundToInt(headingAngle);
    29.  
    30.         //Set the text of Compass Degree Text to the clamped value, but change it to the letter if it is a True direction
    31.         switch (displayangle)
    32.         {
    33.         case 0:
    34.             //Do this
    35.             CompassDirectionText.text = "N";
    36.             break;
    37.         case 360:
    38.             //Do this
    39.             CompassDirectionText.text = "N";
    40.             break;
    41.         case 45:
    42.             //Do this
    43.             CompassDirectionText.text = "NE";
    44.             break;
    45.         case 90:
    46.             //Do this
    47.             CompassDirectionText.text = "E";
    48.             break;
    49.         case 130:
    50.             //Do this
    51.             CompassDirectionText.text = "SE";
    52.             break;
    53.         case 180:
    54.             //Do this
    55.             CompassDirectionText.text = "S";
    56.             break;
    57.         case 225:
    58.             //Do this
    59.             CompassDirectionText.text = "SW";
    60.             break;
    61.         case 270:
    62.             //Do this
    63.             CompassDirectionText.text = "W";
    64.             break;
    65.         default:
    66.             CompassDirectionText.text = headingAngle.ToString ();
    67.             break;
    68.         }
    69.     }
    70. }