- Home /
Compiler error while trying to access a Collider's script.
I have this script, MintScript.cs:
using UnityEngine;
using System.Collections;
public class MintScript : MonoBehaviour
{
public int type;
void OnTriggerEnter(Collider collision)
{
collision.gameObject.GetComponent("Collectables").addMint();
Debug.Log(collision.gameObject.name);
...
}
}
Attached to that Collider (the player), I have a script called Collectables.cs.
When I try to compile, I get this error:
Assets/Scripts/MintScript.cs(15,67): error CS1061: Type UnityEngine.Component' does not contain a definition for
addMint' and no extension method addMint' of type
UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)
Any idea why this is happening? The Collider's name matches my character's object name, tested by that log statement, and that character definitely has the Collectables script attached. Both are C# files.
Answer by Eric5h5 · Mar 18, 2013 at 01:56 AM
Don't use strings in GetComponent.
collision.gameObject.GetComponent<Collectables>().addMint();
or
(collision.gameObject.GetComponent(typeof(Collectables)) as Collectables).addMint();
As the error says, Component does not contain addMint, so you need to cast to the correct type.
Your answer
Follow this Question
Related Questions
Open door = load scene c# issues 4 Answers
Multiple Cars not working 1 Answer
Object reference not set to an instance of an object 2 Answers
OnTriggerEnter throws an error but code still works 1 Answer