- Home /
help with script
hi everyone i just need some help with my script. its supposed to make a pickup that can only be picked up when it has collided with the player and the mouse button has been pushed how ever it dosent work. here is the script attached to the player:\
function OnTriggerEnter(other : Collider){
if (other.CompareTag("Paper")) {
this.GetComponent(destroyandcall).enabled=false;
}
}
function OnTriggerExit(other : Collider){
if (other.CompareTag("Paper")) {
this.GetComponent(destroyandcall).enabled=true;
}
}
and here is the script on the pickup:
var batteryPower : int = 1; //The amount of points to give, negative value takes away points instead
function OnMouseDown () {
notecount.AlterEnergy(batteryPower);
Destroy (gameObject);
}
please answer if you can thanks.
Non- sleeping rigidbody on at least one of the things involved with the Trigger?
Answer by ZenithCode · Oct 01, 2012 at 10:25 AM
Which part of the script does not work?
Can you do some debugs?
I believe this does not work on JS: this.GetComponent(destroyandcall).enabled=false;
So what you need to do is DestroyComponent and re-adding it when you need it.
$$anonymous$$g Remove: Destroy(this.GetComponent(destroyandcall));
Add: this.AddComponent(destroyandcall);
There is no need to Add and Destroy components to enable them.
Answer by whydoidoit · Oct 05, 2012 at 06:33 AM
Your problem is that you are doing a GetComponent on the Player and not on the pickup.
Your code should look like this:
function OnTriggerEnter(other : Collider){
if (other.CompareTag("Paper")) {
other.GetComponent(destroyandcall).enabled=false;
}
}
function OnTriggerExit(other : Collider){
if (other.CompareTag("Paper")) {
other.GetComponent(destroyandcall).enabled=true;
}
}
Answer by soulblade · Oct 05, 2012 at 07:31 AM
too late I had a brainstorm earlier and i found the answer myself. I removed the script on the player and added a sphere under the pickup, renderd the sphere invisible and set it to is trigger. then I added the players script to the sphere changed it to this:
var ranOnce : boolean = false;
function OnTriggerEnter(other : Collider){
if (other.CompareTag("Player"))
if (!ranOnce)
{
ranOnce = true;
transform.root.gameObject.AddComponent(destroyandcall);
}
}
function OnTriggerExit(other : Collider){
if (other.CompareTag("Player")) {
Destroy(transform.root.gameObject.GetComponent(destroyandcall));
}
}
and it works perfectly!
Your answer
![](https://koobas.hobune.stream/wayback/20220613081722im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
OnCollisionStay for seconds then destroy 0 Answers
Destroy a GameObject 2 Answers
Overlap Detection HELP!! 0 Answers
Change scene with trigger collision not working. 1 Answer
Collsion without ririgibody 1 Answer