- Home /
'UnityEngine.Component.collider' is obsolute,'UnityEngine.Component.Collider' is obsolete
I am new to unity and I'm currently following a tutorial by a guy called Brackeys. He explains everything very well, but some of his code seems to be outdated. This is his code: void OnCollisionEnter (Collision collisionInfo); { Debug.Log(collisionInfo.collider.name); }
I am trying to get the name of the object that my player has collided with. But because of outdated code this doesn't work anymore. This is what I tried to fix it: void OnCollisionEnter(Collision collisionInfo) { Debug.Log(collisionInfo = GetComponent(name)); }
This also doesn't work. I am aware that I should use GetComponent (because of the error log inside unity), but I'm not sure how to use it. Thanks in advance.
Your error does not apply to the portion of code you've posted. The OnCollisionEnter callback's parameter is of type Collsion, not Collider. [Collision.collider][1] is not obsolete. Please check the line numbers carefully and show more context to your problem.
Your "fix" just makes no sense. The original code you've posted works fine.
However in case you actually try to access the obsolete collider property of the Component base class, ins$$anonymous$$d of
someObject.collider.name
you have to use
someObject.GetComponent<Collider>().name
Though such a line actually wouldn't make much sense as a Collider doesn't have it's own name but share the name of the gameobject it's attached to.
Again, you should post the actual code that is giving this error and provide more context. Feel free to edit your question. [1]: https://docs.unity3d.com/ScriptReference/Collision-collider.html
Answer by Bunny83 · May 03, 2018 at 08:21 PM
Well the issue is that you named your own script Collision
. Therefore the parameter in your OnCollisionenter method does not use UnityEngine.Collision
but your own Collision class.
In general you should avoid using the same name as built-in classes as it just creates tons of issues. So the best solution is to rename your own Collision class (and file) to something else. However if you want to keep that name you have to use the fully qualified class name in your OnCollisionEnter method:
void OnCollisionEnter(UnityEngine.Collision collisionInfo)
{
Debug.Log(collisionInfo.collider.name);
}
Answer by joris_unity433 · May 03, 2018 at 08:08 PM
Update: I still haven't found out how to fix the problem, but I will give more insight into my code in this update due to a request. In my game I have an object called Player, it moves on top of a surface which has no friction. The player can move left and right and it needs to dodge obstacles located on the surface. I want my player to reset if it hits the obstacle. So first I need to check whether the player hits something and after that I need to check if the object that my player has collided with is an obstacle. And after that it needs to reset. Her comes my code: this is the collision script
**using UnityEngine;
public class Collision : MonoBehaviour {
void OnCollisionEnter(Collision collisionInfo)
{
Debug.Log(collisionInfo.collider.name);
}
}**
And this is the exact error log.:
Assets/Collision.cs(8,33) : error CS0619: ' UnityEngine.Component.collider ' is obsolete: ' property collider has been deprecated. Use GetComponent() instead. (UnityUpgradable)'
I hope that this is enough info, Thanks in advance! @Bunny83
You know that there is an edit button in the cogwheel menu on the top right of your question? You just posted an answer which doesn't answer the question.
I Have copied the whole script from this page: *https://docs.unity3d.com/ScriptReference/Collision-collider.html*
And it still doesn't work. The error log keeps telling me that the property "collider" doesn't exist anymore, that it's deprecated. And if I switch to the 2018-2b version, it tells me that it is just a base class for all colliders.
Your answer
Follow this Question
Related Questions
Why The player don't move? 0 Answers
BCE0019: 'text' is not a member of UnityEngine.component 2 Answers
i need help with make an attack animation only when the player is near 1 Answer
How to make a 3D character move? 4 Answers
Simple - Visual Studio Code not loading script when icon is clicked through unity *Video provided* 0 Answers