- Home /
add is not a member of UnityEngine.Component
I know many of members have asked this question, I have tried to apply their solutions but they are not working for me. I have an object with this script :
function OnCollisionEnter (col : Collision) { if (col. gameObject.name == "Player"){ var go = GameObject.Find("Player"); go.GetComponent(testToAdd).add(); print("You have the key"); Destroy(gameObject); }
}
When Player hits the object, object gets destroyed and calls a function "add" in script "testToAdd.js", whitch is attached to Player. I'm using Unity 3.2 Pro Trial and try to build game on iPad, when receive the error from topic...
Script containing add function looks like this :
function add () { gameObject.AddComponent("playerCollisionForDoor"); }
Answer by Valdemars Magone · Feb 28, 2011 at 12:24 PM
I found another solution, I attached this script to Player and changed it :
function OnCollisionEnter (col : Collision) { if (col. gameObject.name == "key"){
gameObject.AddComponent("playerCollisionForDoor"); print("You have the key"); // Destroy(gameObject); }
}
Before that this script was attached to gameObject Key...
Answer by DaveA · Feb 25, 2011 at 04:03 PM
Try this:
var test2add : testToAdd = go.GetComponent(testToAdd);
test2add.add();
Answer by efge · Feb 25, 2011 at 04:03 PM
I think it is better to declare variables:
var playerScript : testToAdd;
playerScript = go.GetComponent(testToAdd);
playerScript.add();
Better (but untested): use the information you get from collision, GameObject.Find is not necessary:
function OnCollisionEnter (col : Collision) {
if (col.gameObject.name == "Player"){
var playerScript : testToAdd;
playerScript = col.gameObject.GetComponent(testToAdd);
playerScript.add();col.gameObject.GetComponent(testToAdd).add();
print("You have the key");
Destroy(gameObject);
}
}
Anyway I receive error : 'add' is not a member of UnityEngine.Component.
Answer by Eric5h5 · Feb 25, 2011 at 05:20 PM
Use generics; also there's no reason to use GameObject.Find here since you already have a reference to that game object:
function OnCollisionEnter (col : Collision) {
if (col.gameObject.name == "Player") {
col.GetComponent.<testToAdd>().add();
print("You have the key");
Destroy(gameObject);
}
}
Error: "'GetComponent' is not a memberof UnityEngine.Collision. Did you mean 'gameObject'?"
col.gameObject.GetComponent.().add() he probably means.
Answer by Hei · Feb 27, 2011 at 12:48 AM
You find object script in Start ().
var playerObject : GameObject; var testToAddScript : testToAdd;
function Start () { testToAddScript = playerObject.gameObject.GetComponent("testToAdd"); }
function OnCollisionEnter (col : Collision) { if (col.gameObject.name == "Player") { print("You have the key"); Destroy(gameObject); } }
Your answer
Follow this Question
Related Questions
Calling Function in other Script via Touch => iOS Crash 1 Answer
What is the best way to call a function from another script 1 Answer
Have problem using Application.OpenURL() opening tel URL scheme on iOS to call a number with symbols 1 Answer
Call function after script compilation? 2 Answers
How to check if a function is called? 2 Answers