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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Add Transform to GameObject editable in inspector

Discussion in 'Scripting' started by Imag-ing, Oct 6, 2020.

  1. Imag-ing

    Imag-ing

    Joined:
    Sep 22, 2020
    Posts:
    3
    Hi everyone!

    I would like to attach a Transform to a GameObject that would be editable in the inspector.
    But not in the way "I have to add an existing Transform in the field "public Transform" " but more in the way "the transform is ALWAYS on the inspector, and I can modify it with arrows of movement and rotation".
    Something like an anchor.

    It is because I have components that would always need this, and I don't want to create too much GameObject ( I have a lot of component that would need this, and if i have to create 1 new GameObject per GameObject already existing, modify it, and then drag it in the field, it would be too long and really boring...).


    Is there a way to achieve that?

    I hope it's understandable!

    Thanks for all!
    Best Regards,

    R. Schim.

    (I've made researches, but I lack this kind of vocabulary, and can't find the good words to find it...)
     
    Last edited: Oct 6, 2020
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Every GameObject automatically has a Transform attached. You cannot add a second Transform to the same object.

    You can create Vector3 variables and choose to interpret them as position, rotation, etc. in your code, if you want.

    If you back up and explain the end result you are trying to accomplish, someone here might be able to help you find a better approach.
     
  3. Imag-ing

    Imag-ing

    Joined:
    Sep 22, 2020
    Posts:
    3
    Thanks for your answer!
    I didn't thought about Vector3. But would it be possible to have a visualisation of where the vector3 is?

    I want to create a "tutorial to build an object composed with a lot of object that need to be assembled together".
    You have a all the object disasembled, and you pick 1 item, and you place it at the good place.
    So, in my case, my need would be to have the final pos of the object without having to recreate a new gameObject everytime.

    Thanks a lot!!

    EDIT 1 : I just found about gizmos.
    with 'OnDrawGizmos', I can add gizmo to the position property and maybe this is a way to go...
    EDIT 2 : Nope, it doesn't work. Gizmos are always displayed, so it would be unusable to have so much gizmos draw in such little place
     
    Last edited: Oct 6, 2020
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    If you want to manually position each component using the scene view, using a separate game object for each is probably the way to go. Even if you could create an embedded editor like you describe, that doesn't sound like it would really be any significant reduction in the amount of work you need to do.

    If possible, you should think about positioning your things algorithmically in a script, rather than positioning each one by hand.
     
  5. Imag-ing

    Imag-ing

    Joined:
    Sep 22, 2020
    Posts:
    3
    Thanks for your answer!
    If this is the only way, I'll do it like this!

    Thanks again!

    Best Regards
     
  6. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You could use
    OnDrawGizmosSelected
    instead.
    It will only display gizmos when you select the GameObject.
     
    adamgolden likes this.