20

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 reference something in the scene, you need to get that reference at runtime. There are several ways to do that. Attach the reference to the object which ...


12

You only need to destroy the GameObject. By destroying the GameObject (Destroy(this.gameObject);), you also destroy the script (Destroy(this)) automatically. Destroying the script simply removes the component from the GameObject. Destroying the GameObject removes all of its components, and the GameObject itself. But there is an issue with your code. ...


10

since you admittedly don't have much experience with 3D and (presumably) OpenGL, I'll give you a "bird's eye" overview of the process. I'll do my considerations about OpenGL, but the basic reasoning yields for other APIs too. When you render something with a modern version of OpenGL you create objects that will reside into the GPU memory, and then mostly ...


8

As requested, one possible solution (with some flaws) is to use raycasting: Attaching a (C#) Script similar to this to the GameObject from which you want to check visibility would work: if(renderer.isVisible) //Check if Camera is turned towards the GameObject first { RaycastHit hit; // Calculate Ray direction Vector3 direction = Camera.main....


8

You're assigning the value of money to the print return value, not the actual int value being returned. money=print(PlayerPrefs.GetInt("Money")); Should be money=PlayerPrefs.GetInt("Money");


7

Destroying the object gets rid of it completely. You cannot get it back. It is gone. Deactivating it just disables it; everything is still there, it just does nothing. Therefore, if you want to reuse the object, you can deactivate it, but if you will never use it again, you should destroy it (and get back its memory).


7

At the moment I'm using std::shared_ptr to support multiple ownership of GameObjects so that they are held by both the scene and any other GameObjects within the game Don't do that. shared_ptr is often the wrong tool for the job, and that certainly applies here. Remember that smart pointers are for managing ownership; shared_ptr is about sharing ownership. ...


6

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 anywhere to the right of the player within a certain range, even if it is at a different height You want true distance, not just the one that is closest in ...


6

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 in only one scene. But what you can do instead is: Ugly workaround 1: Have the ScriptableObject find its object(s) at runtime. You can do that by tag, by name ...


5

Depending on your exact needs, another possible solution would be to do a test render where you set different objects to different colors and then check for that color in the test render. However this would only be useful in pretty obscure situations; in the majority of situations I would use raycasting. I'm just dropping in this different answer for ...


5

You can Destroy the component. Be careful about which object you destroy, though. If you pass a GameObject to Destroy, you will destroy the entire thing. To destroy the component, you must pass a reference to that component specifically. //example: destroys the MeshRenderer attached to this GameObject var sphereMesh = GetComponent(MeshRenderer); Destroy(...


5

Passing through the gameObject name to a method, then using GameObject.Find to find it again by its name, is bad practice, and inefficient. GameObject.Find is an expensive operation. Rather do something like this... void Update () { if(Input.GetButtonDown("Fire1")){ // detect left mouse click ray = Camera.main.ScreenPointToRay(Input....


5

I believe an option available to you is to use std smart pointers, more specifically the std::shared_ptr and std::weak_ptr. Typically, those pointers represent ownership. They also offer a ref-counting mechanism, and I think it's this feature that you're after. std::shared_ptrs hold a reference to a dynamically allocated object, a count of "hard" ...


4

Your LevelCollider is attached to a Collider that is a trigger. That means it will not call OnCollisionEnter. It will call OnTriggerEnter. Try adding this to LevelCollider: void OnTriggerEnter(Collider other) { Application.LoadLevel("Level1"); } That should do ya!


4

Here are the steps for applying scripts to multiple objects. Step 1. Select all your objects from Hierarchy panel. Step 2. Then from Project panel drag & drop your script into Inspector panel.


4

object_index This read only variable returns the index of the object that the instance has been created from. So your code would read: if(object_index == obj_square) { // ... }


4

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 like this: using UnityEngine; using UnityEngine.EventSystems; public class Clickable : MonoBehaviour, IPointerClickHandler { public void OnPointerClick( ...


4

For an instantaneous radius check, you can use the OverlapSphere method like so: Collider[] hits = Physics.OverlapSphere( explosionCenter, explosionRadius, layerMaskToCheck ); This gives you an array containing all colliders touched by the explosion's spherical volume. You can then iterate through that array to apply forces &...


4

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 different layer. You can do this through code by changing the gameObject.layer. Then, in a camera's Inspector, deselect that layer from the "Culling Mask" drop-...


4

Add to your music class a method like this: public static Music GetInstance() { // Optionally, you can create an instance here // if one isn't already present in the scene. return instance; } Then when you want to toggle the music, instead of referencing an object saved in a member variable, ask the music class which instance is current: var ...


4

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. You might also have other groups of audio sources you want to mute together. In that case you might want to use the cleaner solution using the Audio Mixer ...


4

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 "functions" that can be applied to it. For example item : ["destroy", "pick up", "eat", "cook"] and then the actor ...


3

These are not the same. Resources.Load is going to create a new uninstantiated GameObject. This means your first example is going to create a new game object, then set that object equal to the new game object created by Resources.Load. That means two objects are going to be created, an empty game object added to the scene (from the new GameObject() call) ...


3

You won't get away on this one with an easy answer. The raycast method is completely flawed. Its the same as calling the object "one pixel" wide and checking if this pixel is visible or not. Unity uses a precalculated visibility matrix, composed with regular sectors of your world, and a quadratic ray cast test. Which has the same flaws but you don't need to ...


3

It sound to me as though you already have an entire map of the town drawn up and you are trying to break it down into 16px X 16px tiles for the sole purpose of adding triggers and colliders etc. Why not just keep the map as its own object and put your collider and trigger objects over it where you need them to be? Really, a tile based map might be better ...


3

You seem to have rectangles that are sandwiched: You have rectangles A, B and C A directly above and is overlapping B, which is above and overlapping C. The update for A is done, pushes it a bit up. The update for B is done, pushes it a bit up because of C, and a bit down because of A. B hasStopped because it did not move because your [x|y]Shift is ...


3

You can use the OnBecameInvisible callback for that. Just implement something like: void OnBecameInvisible(){ // disable or destroy gameobject } The method will get called when the object isn't being rendered by any camera anymore. This also includes cameras from the Unity-Editor. So if you're running your game and have the Game and the Scene view ...


3

That's not using GetComponent() on the class GameObject, it's using GetComponent() on the result of GameObject.Find() GameObject.Find() is a static function, but it returns a specific object. Note that Code 2 is also using GameObject.Find() but then you store the result object in a variable. Code 1 uses the exact same functions but doesn't store the result ...


3

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 correlated, except for one case. And that one case is rendering. When you scale an object (especially non-uniformly) then it cannot be batched with other like-...


3

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. Translation( position ), Rotation and Scaling. Have a look in detail from these answers Right terms are so important because of there generality. For example ...


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