24 votes
Accepted

How to store references to scene objects in prefabs?

In the editor, prefabs can only reference other prefabs. Prefabs are supposed to be shared between scenes. That means they can't rely on the object being present in every scene. If you need them to ...
Philipp's user avatar
  • 119k
12 votes
Accepted

Unity3d - Do I need to destroy gameobject AND script?

You only need to destroy the GameObject. By destroying the GameObject (Destroy(this.gameObject);), you also destroy the script (...
Evorlor's user avatar
  • 5,793
7 votes
Accepted

ScriptableObject referencing a gameobject in scene

Sorry, but that's not going to work. ScriptableObjects are supposed to act as assets which can be reused between scenes. That means you can't and shouldn't make them dependent on objects which exist ...
Philipp's user avatar
  • 119k
6 votes
Accepted

How to inspect/modify the runtime state of a node in the Godot editor?

You can do so, although Godot's behavior is slightly different to Unity and I'd suggest trying it in a small test project before you unintentionally change an actual working project. The functionality ...
Mario's user avatar
  • 8,442
5 votes
Accepted

In GameMaker how to check if instance is a given object?

object_index This read only variable returns the index of the object that the instance has been created from. So your code would read: ...
Quentin's user avatar
  • 1,178
5 votes
Accepted

How to find nearest gameobject on one side

The best way to do this depends on a few things. I'm going to assume the following - if any of these are incorrect, please let me know and I'll update my answer: You want the closest enemy that is ...
Cameron Oltmann's user avatar
4 votes
Accepted

How do I find the location of a GameObject in C#

Basically its all the game of terminologies, probably you couldn't find anything because of it. Actually the basic term is Transformation which includes three basic attributes of any object, i.e. ...
Hamza Hasan's user avatar
  • 3,377
4 votes

How to store references to scene objects in prefabs?

Unity does not allow you to reference scene objects to prefabs. It allows any other reference. It's because prefabs have a template-like nature and thus are unaware of the scene. There are many ...
Bizhan's user avatar
  • 169
4 votes
Accepted

How can i detect if the player is inside an explosion radius?

For an instantaneous radius check, you can use the OverlapSphere method like so: ...
DMGregory's user avatar
  • 134k
4 votes
Accepted

Is there a way to make a game object invisible to only certain players?

You can specify layers that a camera should and should not render: https://docs.unity3d.com/Manual/Layers.html. Anything that should be rendered by one camera but not the other should be assigned to a ...
tyjkenn's user avatar
  • 2,586
4 votes
Accepted

I need to keep music playing but still be able to mute it between games, how?

Add to your music class a method like this: ...
DMGregory's user avatar
  • 134k
4 votes

I need to keep music playing but still be able to mute it between games, how?

The solution by DMGregory is a quick and dirty solution which works well for a single audio source. But in a more complex game you might have multiple audio sources which function as background music. ...
Philipp's user avatar
  • 119k
4 votes

Looping through children in a foreach loop

By itself, the transform class implements the IEnumerable (with Transform as the generic type) interface, which allows you to use a foreach with a transform as the interable object. You could also use ...
LifGwaethrakindo's user avatar
4 votes
Accepted

Storing transformations in game objects. (GLM, C++)

My general recommendation is to keep both the source components of your transformation for ease of manipulation: local translation vector local orientation quaternion local axis-aligned scale triplet,...
DMGregory's user avatar
  • 134k
4 votes
Accepted

Management of objects as targets of other objects

I believe an option available to you is to use std smart pointers, more specifically the std::shared_ptr and ...
Vaillancourt's user avatar
  • 16.3k
4 votes
Accepted

How "smart objects" are implemented and actually work?

Well, concerning "The Sims" article you are reffering to that actually could mean a lot of different things. But the core idea as I get it is that each object can expose a list of "...
Petr Abdulin's user avatar
  • 2,711
3 votes

How to make Lists (and Arrays) in Unity only take GameObjects of a certain "type"

If you only want to store GameObject types that contain a specific Component element, the easiest solution is to use the ...
Gnemlock's user avatar
  • 5,263
3 votes
Accepted

On Click Add Listener not working on Game Object

You are using a button, which is an UI element. It is not supposed to be used on 3d objects in the game world. On 3d objects in game world you probably want to use IPointerClickHandler. They work ...
Lasse's user avatar
  • 3,241
3 votes
Accepted

How to get which gameobject the mouse is over in Unity?

Physics.Raycast will help you to get the gameobject using mousePosition and ScreenPointToRay ...
Muhammad Faizan Khan's user avatar
3 votes
Accepted

Does distant and large objects use up a lot CPU usage?

Aside from my comment (don't have the flexibility to pull back a specific post at the moment) the answer to your question: No, but... There is no CPU gain or loss by changing scale. It's not even ...
Draco18s no longer trusts SE's user avatar
3 votes

How do I find the location of a GameObject in C#

To get the position of a gameobject in the world, you can retrieve a Vector3 (x,y,z) of it's Position from the Transform component that's added by default to every gameobject, as seen in the inspector:...
Happy Apple's user avatar
3 votes
Accepted

How deep does the GameObject.GetComponentsInChildren() , Go?

The documentation you link covers the answer to this, quite clearly. GetComponentsInChildren will return components from all sub children. This is the API ...
Gnemlock's user avatar
  • 5,263
3 votes

How to have multiple GameObjects sharing pieces of same material?

Have you looked into using Unity's Shadergraph to make your own shader for the material? You might be able to achieve what you want by using the worldspace position for texture/uvs. I am not good at ...
faulknerj's user avatar
3 votes
Accepted

Is it more efficient to add scripts to each GameObject or to a single parent object?

There is some amount of overhead to invoking each script's Update() and other methods, compared to maintaining a list of objects and updating them all at once from ...
DMGregory's user avatar
  • 134k
3 votes

Using StartCoroutine() and WaitForSeconds()

This is not how coroutines work. StartCoroutine returns immediately after starting the coroutine, and the function you called it from keeps executing. It does not wait for that coroutine to finish. ...
DMGregory's user avatar
  • 134k
3 votes
Accepted

Can a Component.gameObject be null in Unity?

It depends. Component.gameObject refers to the gameObject that the component (most likely your script) is from. Therefore if you are referencing the 'gameObject' that the script is attached to it ...
Daniel Wilkes's user avatar
3 votes

Do inventory items have to live in the game world?

I've figured out that the reason that I couldn't populate the inventory with only data and needed to be on Gameobject was cause the class was inheriting from Mono-behavior which REQUIRES it to be on a ...
Kimbur's user avatar
  • 49
3 votes
Accepted

Alternative for using game object/prefabs for level generator

Tile map is one option to use, the other is to maintain only the number of tiles that can be seen on screen at any given moment, and copy the tiles that go offscreen to the location of the next tiles ...
nihohit's user avatar
  • 595
3 votes
Accepted

disabling or enabling a public component

Solution Try replacing your Component type to a MonoBehavior. Then script.enabled = false ...
Charly's user avatar
  • 742
3 votes

I need multiple custom game objects to stick to a parent object

Say you have two GameObjects p and c. You can make c a child of ...
Kevin's user avatar
  • 5,734

Only top scored, non community-wiki answers of a minimum length are eligible