Whenever my object touches the finish line, money is added
This is my script, I'm trying to have a character that touches the finish line and then generates money. I tried using tag instead of name and tried adding the script to the "finish" line or only to the character or both of them but nothing seems to work. "Wash" is my anterior script for generating money when clicking on the character.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Finish : MonoBehaviour {
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "finish")
{
Wash.money=10;
}
}
}
I checked the script for wash and it works, the problem seems to be at the collision code
Try using a trigger ins$$anonymous$$d of a collision and then use OnTriggerEnter()
Answer by iZebbe · Jan 06, 2019 at 11:59 AM
You should use a collider on the gameobject you use as a finish line, ex. a box collider instead of the collision solution you are currently using. Make sure you check the Is Trigger box as true on your collider attached to your finishline. You then need to add a collider to your player, make sure the Is Trigger is not set on your player. You also need to add rigidbodies to both your player and your finishline gameobject. Make sure you disable gravity on both objects, if not they will fall through your scene. You then need to replace your code attached to your finishline:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Finish : MonoBehaviour {
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "finish")
{
Wash.money=10;
}
}
with:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Finish : MonoBehaviour {
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "finish")
{
Wash.Money +=10
}
}
}
It worked thanks a lot, really helped me.
I kiss your neck bro
xoxo
Your answer
Follow this Question
Related Questions
why isnt my code working, 2 Answers
ANIMATION TRIGGER DOESN'T PLAY 1 Answer
Particle Collision between particles 0 Answers
Checking for a gameobject at a position 1 Answer
My main character isn't being able to colide with the objects. 1 Answer