Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Concatenate String to call Vector3

Discussion in 'Scripting' started by matClark, Jan 23, 2023.

  1. matClark

    matClark

    Joined:
    Jan 18, 2022
    Posts:
    5
    HI,

    hope that someone might be able to help me out.

    I'm new to c# scripting but I managed to write a script that smoothly moves a camera from one defined target position and rotation to another, but it was bulky with a coroutine for each target.

    I figured that I should be able to concatenate (is that the correct word?) a string to help define the name of the Vector3 to be called - see lines 18 & 25 in the example - but it throws the "Cannot implicitly convert type 'string' to 'UnityEngine.Vector3" error at me.

    Hopefully someone will be able to help me sort it out.

    Thanks in advance,

    Mat :)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class moveCamRefined : MonoBehaviour
    5. {
    6.     public GameObject objectToMove;
    7.     public float moveDuration = 1;
    8.     public GameObject target1;
    9.     public GameObject target2;
    10.     private Vector3 targetPosition;
    11.     private int key;
    12.     void Update()
    13.     {
    14.         if (Input.GetKeyDown("1"))
    15.         {
    16.             print("the one was pressed");
    17.             key = 1;
    18.             targetPosition = "target" + key + ".transform.Position";
    19.             StartCoroutine(moveObject(targetPosition, moveDuration));
    20.         }
    21.         if (Input.GetKeyDown("2"))
    22.         {
    23.             print("the two was pressed");
    24.             key = 2;
    25.             targetPosition = "target" + key + ".transform.Position";
    26.             StartCoroutine(moveObject(targetPosition, moveDuration));
    27.         }
    28.     }
    29.     //COROUTINE
    30.     //move to targetPosition
    31.     IEnumerator moveObject(Vector3 targetPosition, float moveDuration)
    32.     {
    33.         float time = 0;
    34.         Vector3 startPosition = objectToMove.transform.position;
    35.         while (time < moveDuration)
    36.         {
    37.             objectToMove.transform.position = Vector3.Lerp(startPosition, targetPosition, time / moveDuration);
    38.             objectToMove.transform.rotation = Quaternion.Slerp(objectToMove.transform.rotation, target1.transform.rotation, time / moveDuration);
    39.             time += Time.deltaTime;
    40.             yield return null;
    41.         }
    42.         objectToMove.transform.position = (targetPosition);
    43.     }
    44. }
     
  2. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    What do you mean by 'The name of the Vector3 to be called'?

    The reason you're getting the error is because you're trying to put a String-value (text) into a Vector3-object (3 floats), which isn't possible. The value you're placing into targetPosition should be a Vector3, not a Position.
     
  3. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Are you trying to set targetPosition to either target1.position or target2.position?
    If so, try something like this:

    Code (CSharp):
    1. private GameObject[] targets = new GameObject[] {target1, target2};
    2.  
    3. if (Input.GetKeyDown("1")) // You should really use KeyCodes instead of a string here
    4. {
    5.     targetPosition = targets[0].transform.position;
    6. }
    7. if (Input.GetKeyDown("2"))
    8. {
    9.     targetPosition = targets[1].transform.position;
    10. }
     
  4. matClark

    matClark

    Joined:
    Jan 18, 2022
    Posts:
    5
    Hi FrankvHoof,

    thanks so much for your reply.

    I never thought of using an array to define the Vector3 - D'oh!....I'm so new to this!!

    I'll make sure to use keycodes from now on too!!

    I'll make those changes now,

    Mat :)
     
  5. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    It looks like you were trying to perform reflection: using runtime data to access members (amongst other things).

    This is absolutely possible! It's also a bad idea:
    • Reflection is slow. The compiler doesn't know what you're going to do ahead of time, so it can't emit efficient code.
    • Reflection is unsafe. The compiler can't prove much about your runtime data, so it can't check if you are using a valid name.
    • Reflection is annoying to write. You lose a lot of conveniences, like accurate code completions and renaming.
    (it also doesn't look much like what you've written -- this is just the idea :p )

    Of course, it's very reasonable to want to associate names with values. For that, you should use a data structure -- maybe an array, like shown above, maybe a
    List
    if you want to be able to resize it at runtime, or maybe a
    Dictionary
    if you want to use arbitrary keys, rather than just sequential numbers.
     
  6. matClark

    matClark

    Joined:
    Jan 18, 2022
    Posts:
    5
    Thanks Chemicalcrux,

    I'll look into reflection so I know more clearly what not to do in future ;)

    Not sure why I didn't think of an array before, it makes perfect sense and it works!!

    Cheers,

    Mat :)
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,949
    If you're just trying to move a camera, you don't need ANY of this cruft. We have better ways here in 2023!

    Camera stuff is pretty tricky... you may wish to consider using Cinemachine from the Unity Package Manager.

    There's even a dedicated forum: https://forum.unity.com/forums/cinemachine.136/
     
    chemicalcrux likes this.
  8. matClark

    matClark

    Joined:
    Jan 18, 2022
    Posts:
    5
    Thanks Kurt,

    that's good to know.

    My main reason for doing this is to develop an understanding of scripting in Unity, I'lll definitely be looking into Cinemachine in the near future though.

    Cheers,

    Mat :)