Auto-Establish Component References in the Same GameObject
See original GitHub issueSummary
With a special (optional) RequiredComponent-Attribute for fields you needn’t assign the required component in the OnInitialize-Method manually.
Example:
[RequiredComponent]
private Spaceship ship;
public void OnUpdate()
{
ship.DoStuff();
}
Analysis
- Type less code
- Do we need an new RequiredComponentAttribute-Class just for fields?
Issue Analytics
- State:
- Created 7 years ago
- Comments:11 (11 by maintainers)
Top Results From Across the Web
Referencing a script on a GameObject in a MonoBehaviour ...
First select the game object with the components you need to connect to your other script. Then lock one of the inspectors with...
Read more >Using GetComponent to Reference a Game Object's ...
Using GetComponent to Reference a Game Object's Component in Unity 3D · Comments34.
Read more >The GameObject-Component Relationship
The Transform Component is critical to all GameObjects, so each GameObject has one. But GameObjects can contain other Components as well. The Main...
Read more >Best practice for getting references to specific child objects ...
So by using GetComponentInChildren you can find that component on the first child that has it. In case you actually need the game...
Read more >unity game engine - Access component from another script
Drag&Drop the according GameObject in the field it automatically gets the according component reference.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Personally, I would only want this feature if it worked in the last way you mentioned (runs when adding or removing a component). Even the metadata version seems like a lot of code to be ran at runtime behind the scenes with no obvious indication to users of its impact. I could see people assuming it would have far less overhead - expecting Duality to work more magic behind the scenes. Seems risky especially with this feature being so convenient that users could end up using it very often.
Couldn’t the procedure you mentioned for the metadata implementation be simplified? If the RequiredComponent attribute already necessitates a similar process, it seems like you could re-use the GetType and maybe the Dictionary lookup if the data for the RequiredComponent and AutoRef attributes were stored together. If the AutoRef is allowed to be null this does have the design flaw of mixing hard requirements and optional setup. Maybe name the attribute RequiredRef rather than RequiredComponent? Or changes the semantics of the implementation to be “component initialization steps” rather “component requirements”. Anyways, if the additional GetType and Dictionary lookup can be shared with RequiredComponent, the additional overhead of this attribute would start to get quite close to the manual implementation and less of an issue. This would make this issue a much more substantial change since it’d be touching the RequiredComponent stuff.
Problem with the RequiredRef idea: what would be the meaning/expectation if RequiredRef was used without RequiredComponent? We can’t prevent this at build time and so it would have to be allowed.
As long as AutoRef is just syntactic sugar for GetComponent inside OnInit I can’t think of a compelling reason to use it over GetComponent. And if it was syntactic sugar, I can see many people preferring the manual way just to be explicit about their initialization steps. The AutoRef attribute would be moving the code relevant to initialization to multiple places.
Another less appealing but far easier to implement implementation would be to have the attribute be an editor only feature - when a component is added to an object in the editor that has the AutoRef property, it automatically fetches a component of the requested type and serializes the reference. At runtime the AutoRef attribute would do nothing.
Below you can find the code for a basic injectioncontainer that will inject values based on either a constant value or a expression.