Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

[Workflow Usability]"Virtual" GameObject "This Object"

Discussion in 'General Discussion' started by darkhog, May 5, 2015.

  1. darkhog

    darkhog

    Joined:
    Dec 4, 2012
    Posts:
    2,219
    Similarly to None, it would be "virtual" as in not existing actually game object that would always point to owner of component in which it was used. It would be useful in scripts that expect GameObject to be selected but user want to affect game object script is in.

    Example: Let say you have script that changes color of specified game object in some intervals:

    Code (csharp):
    1. public GameObject obj;
    2.    public float interval;
    3.    private float intervalcounter=0;
    4. void Update() {
    5.    intervalCounter += Time.deltaTime;
    6.    if (intervalCounter >=interval) {
    7.       if (obj!=null){
    8.       //blablabla, change color here
    9.       }
    10.       intervalCounter=0;
    11.    }
    12. }
    And you want it to affect game object it was used in. Normally you would have to either look through hierarchy tree, then drag to the field or use hardly convenient selector (both are hassle when you have quite complicated scene). But with This Object "virtual" GO, you could just select "This Object" in selector and poof, now we have disco object!

    Another, more down to Earth example: I've often missed such a feature when designing UI and working with Events (always being since 4.6 when UI was released). Most of the time I wanted it just to affect itself (e.g. when making a button, I wanted to send a message to one of scripts that were connected to it, not other objects). And my scenes aren't even THAT complicated (yet).

    Please consider making such a feature.
     
  2. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    941
  3. darkhog

    darkhog

    Joined:
    Dec 4, 2012
    Posts:
    2,219
    Yes, but without writing it in code. You can use None "virtual GO" to erase whatever was in GameObject field in the inspector and set it to null internally, with This Object, it would set it to Component.gameObject internally.
     
  4. Zeblote

    Zeblote

    Joined:
    Feb 8, 2013
    Posts:
    1,102
    Like this?

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Blah : MonoBehaviour
    4. {
    5.     public GameObject Target;
    6.  
    7.     private void Start()
    8.     {
    9.         if (Target == null)
    10.             Target = gameObject;
    11.     }
    12.  
    13.     private void Update()
    14.     {
    15.         Target.stuff();
    16.     }
    17. }
     
    zombiegorilla likes this.
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,787
    Probably belongs in scripting, not general discussion.

    I'm having trouble figuring out your intent or use case. Virtual has a specific meaning, which seems to be nothing like what you are talking about. The solution is probably trivial, if you can accurately describe the functionality you want.

    Are you simply asking about misdirection? This is a pretty common design pattern, you should be able to find plenty of examples on Google.
     
    SunnyChow likes this.
  6. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    941
    If I understand you correctly, you simply want to be able to select the current GameObject in the inspector with a dropdown instead of drag-and-drop? Frankly I don't see much benefit in that, drag-and-drop is pretty easy after all, especially when you already have the current object selected and dont have to hunt for it. It's also really easy to do a check for null in code, this way you can just leave it empty without dragging and dropping anything.

    You could do it with a property

    Code (csharp):
    1.  
    2. class SomeClass : MonoBehaviour
    3. {
    4.     [SerializeField] private GameObject m_SomeObject;
    5.     public GameObject SomeObject
    6.     {
    7.         get
    8.         {
    9.             if (m_SomeObject == null) return gameObject;
    10.             return m_SomeObject;
    11.         }
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         SomeObject.Stuff();
    17.     }
    18. }
    19.  
     
  7. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,311
    As @Zeblote & @BoredMormon pointed out, it is very trivial if it is behavior you want. Adding that functionality at the core would cause unwanted/unexpected functionality.
     
    judah4 likes this.
  8. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    13,557
    What is the problem you're trying to solve here?
     
    SunnyChow and Kiwasi like this.
  9. darkhog

    darkhog

    Joined:
    Dec 4, 2012
    Posts:
    2,219
    Problem of having thousands of objects in the scene and trying drag/drop the one that is currently selected to gameobject property in script in that object. Again, my scenes are not that complicated, but even so, I already have troubles with this. And with scenes on scale of AssCreed game or even Hitman one, well, good luck with that.
     
  10. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    13,557
    As others have suggested, there are plenty of trivial or near-trivial solutions to that. Also, since the .gameObject accessor already exists I don't think that any single said solution would necessarily work for all or even most use cases.
     
  11. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,311
    If you are really putting thousands of objects in a scene manually, you really should consider writing a system to handle them. If nothing else, an editor tool to manage them. It could be as simple as having a good naming convention and using Find to locate and manipulate them (editor or runtime). Proxy classes work well too if you are at a stage where things are still changing.
     
    NomadKing and angrypenguin like this.
unityunity