How to destroy the single colliding object? - c#

Hope you're well. I've been trying to make it so when the bullet collides with the enemy, the enemy dies using the Destory(gameObject); function.
Yet, when it destroys the gameObject, this prevents the enemies from spawning as the spawner cannot find the gameObject.
So, I'd need to make it so Destory(gameObject); only destroys a single enemy bullet hits, not the full gameObject.

your mistake is you are destroying the referenced object, and your other script wants to call it.
do not use a scene object as reference object. make it a prefab, and than drag that prefab on object with script. it will solve your problem.

Related

OnCollisionEnter not working, What is wrong?

Help, im using unity 2020.3.15f2, OnCollisionEnter is not working.
I have 2 gameObjects, both with not trigger SphereColliders and not Kinematic RigidBodies.
My RigidBodies doesn´t have gravity on, but they have constants (don´t move on z, and don´t rotate on x or y)
Im shure they collide because both gameobjects interact colliding, but when i call the script that contains the OnCollisionEnter (that just call a Debug.Log("Collision")) i don´t see anything on console.
void OnCollisionEnter(Collision col){
Debug.Log("Collision"); //i don´t see anything on console
}
This is my SphereCollider and RigidBody setup for both GameObjects:
And the script is on the parent of those GameObjects, like this:
This is the Scene Hierarcy
And this is the "Element" Hierarchy (note: i edit it to mantain the names of my gameobjects in secret its my boss desition)
As you can see here the "Element" its the one who have the script with the OnCollisionEnter
I Solve by thinking: How my scene is setted?, I see that the gameObjects that contain the Collider and the RigidBody, are child of the gameObject with the OnCollisionEnter script.
Aparently, OnCollisionEnter only works with the gameObject itself, if the script is attached to a parent or to a child of the gameObject, it´ll not work.
So with that, i just need to make a Collision Detection Script on the gameObjects with the Colliders, and entangle it with the parent´s script to do... the thinks the gameObject should do when collide.
Thanks all for your feedback and help, special thanks for #derHugo and #Ruzhim who are the people who help me to detect that problem.
Solution: make shure the script with the OnCollisionEnter is on the gameObject that have the Collider and the RigidBody, not the parent, not the child.

How to detect collisions for a rotating child object in Unity3D C#?

I am making a First Person Shooter and I have a rigged player with a mouse look script, and I used to rotate the spine with spine.transform.rotation, but this method does not look for collisions, this means if I rotate and I am standing near to a GameObject, I glitch/bug through the object. So I tried to do it with a Rigidbody but this doesn't seem to work, I tried: rbSpine.AddTorque(new Vector3(mouseLook.y,0,0), ForceMode.Force);. This doesn't do anything. And I have another problem with the Rigidbody, I've only a rigidbody attached to the spine, my player has multiple simple collision and these overlap eachother a little, and my rigidbody is pushed away into the air slowly. Does anyone know a solution for this?
I put a rigidbody component on the player, this will take care of the collision detection for itself and all its child objects.

unity: fix Player passing through objects

After updating from unity 5.5.2 to 2017.3 the Player character does not collide with objects(evironment asset) and is passing through them, all the objects have mesh collider with convex on, This is only happening in one of the level(scene) in the game while in rest of the level(scene) the player behaves normal and is colliding with objects.
Player settings
object settings
The player's rigidbody is kinematic. I would try by disabling that property.

How do non-instantiated loaded Prefabs compare to instantiated ones?

Example
In Unity5, assuming that a GameObject with name "SomeObject" was stored as a prefab at Assets/Resources/SomeObject.prefab, I know that I can create an instance of the prefab as follows:
GameObject prefab = Resources.Load<GameObject>("SomeObject");
GameObject instance = GameObject.Instantiate(prefab);
Once executed, the instance GameObject will be a copy of the original "SomeObject" prefab, and will have been placed in the current active scene with the name "SomeObject(Clone)".
As far as I understand, the GameObject prefab represents the actual prefab asset, and any changes made to it (setting name, adding components, etc) are written to the original prefab asset, and these changes persist even after exiting editor play mode.
Questions
Since all GameObjects are normally stored in a scene, and the scene property on GameObject prefab in the above example seems to be Unloaded/Invalid/Unnamed, where exactly should I consider this special GameObject to be? I seem to be able to do everything with it that I can do with normal GameObjects, short of being visible in the hierarchy/scene view.
Is it effectively in some kind of limbo state, or a special PseudoScene? It seems to persist through LoadSceneMode.Single scene changes, but is not like the special DontDestroyOnLoad scene that Objects are moved to when passed into GameObject.DontDestroyOnLoad(...).
Are there any other noteworthy differences between prefab and instance in the above example, other than lifetime changes, the invalid scene and being different objects from one another (Having different GetInstanceID(), etc)?
Since calling GameObject.Instantiate(...) on prefabyields a GameObject that is in a valid scene, is there any way to manually create GameObjects that are in a similar 'no scene' state? I am aware that Unity intends for all GameObjects to be in scenes and so this is purely an academic question.
I have tried looking through the documentation of the functions involved, but none go into the technical details of what specifically is happening when you call Resources.Load on a prefab resource.
1.Where exactly should I consider this special GameObject to be?
It's in a separate file and not referenced in the scene. Once Resources.Load<GameObject>("SomeObject"); is called, it is loaded into the memory waiting to be used when GameObject.Instantiate is called.
If it is declared as public GameObject prefab; and assigned from the Editor instead of GameObject prefab Resources.Load<GameObject>("SomeObject");, it will be loaded into the memory automatically when the scene loads.
2.Are there any other noteworthy differences between prefab and object?
Yes.
Prefabs cannot be destroyed with the Destroy or DestroyObject function. It has to be destroyed with the DestroyImmediate function by passing true to its second arguement: DestroyImmediate(prefab, true);
3.Is there any way to manually create GameObjects that are in a similar 'no scene' state?
No, there is no way to manually create GameObjects that are in a similar 'no scene' state.
Although you can fake it.
The closest thing to that is to make the GameObject invisible in the Editor's Hierarchy and Inspector tabs by modifying the GameObjects's hideFlags variable then deactivate that GameObject.
GameObject obj = new GameObject("SomeObject");
obj.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
After that, deactivate the GameObject:
obj.SetActive(false);
Now, the GameObject is invisible in the Editor and it is also invisible in the Scene and Game View.
There is really no big difference between prefabs and a typical GameObject. Prefab is just a container that holds the GameObject so that it can be re-used and shared between scenes. Modifying one prefab will update any instance of it.
Imagine when you have hundreds of GameObjects of the-same type in the scene and need to modify all of them. With prefab, you only need to modify one of them or just the prefab then click Apply to update other instances. This is the only thing you should think of prefabs.

Unity attaching GameObjects to scripts as a class

About a month into making my first game, I have realized that I am very confused when it comes to attaching gameobjects as classes to another game object's script. That's a mouthful so here's my issue.
My Game.cs attached to Game (gameobject) has
public Player player;
My Player.cs is attached to a Player (gameobject) prefab
Why can I drag a Player gameobject, that has a player script, into the Game gameobject's "player" field? How does that make sense? It should be
public GameObject player;
Then I would drag my prefab gameobject "Player".
The reason why this confuses me is, If i Instantiate "player", am I instantiating a gameobject? It seems not, because I cannot do this:
GameObject newPlayer = Instantiate(player, new Vector3(1,1,1), Quaternion.identity) as GameObject;
newPlayer.transform.SetParent(GameObject.Find("Level"));
If I do I will get the error the newPlayer is null.
A script is actually a Component object like any other object (most of the time).
It is technically not too easy to drag a Component object from an inspector onto another slot in another inspector. Probably thats why they made this "shorthand", when you can drag the actual GameObject instead from the hierarchy.
If the dragged GameObject has a matching component to a slot, it counts as you have dragged the Component itself.
You cannot instantiate a script Component using Object.Instantiate, you'll always instantiate a GameObject this way, see the docs. If you need the script, simply get the component from the brand new instance.
It can be confusing indeed, I personally prefer naming convention something like:
public GameObject playerObject;
public Player player;
Unity. by default, will cast any gameobject into the possible component, for example, if you have a Transform in your script and pass a gameobject (as a lot of tutorials do), it will automatically cast to the transform of given gameobject.
The main problem with that is simply that if you try to attach an object without Player script, it will probably crash in one way or another if you don't check the value.
Anyway, while you are working with your own scene and are sure the player gameobject has the necessary script, it won't be a problem in any way
Oh, about the last part, attaching components is common for already instantiated objects, to instantiate from a component you should get the parent game object of that script, and it would not make sense as you could easily attach the prefab directly
I don't remember now exactly how, sorry

Categories