{"id":23543818,"url":"https://github.com/beatthat/safe-refs","last_synced_at":"2025-07-28T12:14:43.897Z","repository":{"id":144017216,"uuid":"102978629","full_name":"beatthat/safe-refs","owner":"beatthat","description":"Use safe refs to hold references to Unity components that may be destroyed. An instance of SafeRef\u003cMyComponent\u003e.value will be null if the component or it's GameObject has been destroyed.","archived":false,"fork":false,"pushed_at":"2020-02-19T06:02:23.000Z","size":82,"stargazers_count":1,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-15T08:40:36.592Z","etag":null,"topics":["defensive-programming","null-check","package","unity","unity3d","utility"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/beatthat.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-09-09T19:06:40.000Z","updated_at":"2024-12-15T08:44:25.000Z","dependencies_parsed_at":null,"dependency_job_id":"872ec4c1-45ad-4b94-8165-7655c0b978cb","html_url":"https://github.com/beatthat/safe-refs","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"purl":"pkg:github/beatthat/safe-refs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beatthat%2Fsafe-refs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beatthat%2Fsafe-refs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beatthat%2Fsafe-refs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beatthat%2Fsafe-refs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/beatthat","download_url":"https://codeload.github.com/beatthat/safe-refs/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beatthat%2Fsafe-refs/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267514694,"owners_count":24099992,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-28T02:00:09.689Z","response_time":68,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["defensive-programming","null-check","package","unity","unity3d","utility"],"created_at":"2024-12-26T07:11:44.625Z","updated_at":"2025-07-28T12:14:43.848Z","avatar_url":"https://github.com/beatthat.png","language":"C#","readme":"\u003ca href=\"readme\"\u003e\u003c/a\u003eWhen components keep references to other components, it's easy to end up with null pointer problems. SafeRefs are a simple way to always know whether a component reference is valid.\n\n## Install\n\nFrom your unity project folder:\n\n    npm init --force # only if you don't yet have a package.json file\n    npm install --save beatthat/safe-refs\n\nThe package and all its dependencies will be installed under Assets/Plugins/packages.\n\nIn case it helps, a quick video of the above: https://youtu.be/Uss_yOiLNw8\n\n## Usage\n\nAn instance of SafeRef\u003cMyComponent\u003e.value will be null if the component or its GameObject has been destroyed.\n\n```csharp\nMyComponent someComponent = GetComponentsInChildren\u003cMyComponent\u003e();\n\nvar ref = new SafeRef\u003cMyComponent\u003e(someComponent);\n\n// ref.value will return someComponent\nDestroy(someComponent.gameObject);\n// ref.value will return null\n```\n\n...SafeRefs are structs to handle them accordingly\n\n```csharp\npublic class HandlesStructCorrectly\n{\n  public void SetComponent(MyComponent c) {\n    m_ref = new SafeRef\u003cMyComponent\u003e(c);\n  }\n  private SafeRef\u003cMyComponent\u003e m_ref;\n}\n\npublic class HandlesStructIncorrectly\n{\n  public void SetComponent(MyComponent c) {\n    var ref = m_ref; // ref is a copy of m_ref, no longer a reference to the same thing\n    ref.value = c; // m_ref has not changed\n\n    // the example above is contrived\n    // but just to illustrate the point\n    // that you need to be careful\n    // to avoid editing copies of struct\n  }\n  private SafeRef\u003cMyComponent\u003e m_ref;\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeatthat%2Fsafe-refs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeatthat%2Fsafe-refs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeatthat%2Fsafe-refs/lists"}