- Home /
I tried to make collectible Coins for my Car Game that Increase the Player's Max Velocity every Time one gets collected, but it failed.
I have fixed all Compiler Errors, but it still doesn't work. Instead the Car just clips through the Coins and the Coins just do nothing besides the Rotation Animation.
This is Part of the Coin Script (everything besides the Coin Rotation Animation): using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Coin : MonoBehaviour {
[...]
//The actual Function of the Coin:
public Renderer coinRenderer;
public Collider coinCollider;
public PlayerCar playerCar;
void Start () {
coinRenderer = GetComponent <Renderer>();
coinCollider = GetComponent <Collider>();
}
//The Coin Rotation Animation plays during every Frame:
void Update () {
[...]
}
//The actual Function of the Coin happens only if the Coin collided with the Player:
void OnTriggerEnter (Collider playerCarCollider) {
if (playerCarCollider.CompareTag ("Player") ) {
playerCar.IncreaseMaxVelocity (); //I tried to Access the IncreaseMaxVelocity Method from the Player Script below
coinRenderer.enabled = false;
coinCollider.enabled = false;
StartCoroutine (Reactivate());
}
}
IEnumerator Reactivate() {
Debug.Log ("Before Waiting 2 Seconds");
yield return new WaitForSeconds (2);
Debug.Log ("After Waiting 2 Seconds");
coinRenderer.enabled = true;
coinCollider.enabled = true;
}
}
And this is Part of the Player Car Script (Basically anything besides the Custom Physics Code): //This Script is taken from the Internet, with some tweaks: [...] //http://www.doofah.com/tutorials/unity/bouncy-vehicle-tutorial/
using System.Collections;
using UnityEngine;
public class PlayerCar : MonoBehaviour {
Rigidbody body;
[...]
public float startMaxVelocity = 50;
float maxVelocity;
[...]
void Start () {
/*Set Start Velocity:*/ maxVelocity = startMaxVelocity;
[...]
}
void Update () {
[...]
}
void FixedUpdate() {
[...]
//I tried to access this Method from the Coin Script:
public void IncreaseMaxVelocity () {
maxVelocity += 10;
}
}
This is the Game I mean (It also has a Package where you can Modify the Game directly in Unity (and hopefully see exactly what's wrong with the Coins)): https://gamejolt.com/games/sosasees-1st-car-game/339276 The correct Version of the Public Dev Files is 0.0.2 (not 0.0.1). If you see Version 0.0.1 instead of 0.0.2, it's currently Uploading and you should try again later. The Upload should be finished no later than 1 day after this Question is online. EDIT: This Upload is now finished
Does the car has a collider? I'm on my phone right now, will download your version when I get to my pc if the problem persists
@$$anonymous$$asyan64 Seems like you forgot to set the field "playerCar" on the coins, this is causing your script not to work.
$$anonymous$$y recommendation would be just removing public PlayerCar playerCar;
from your code and replacing playerCar.Increase$$anonymous$$axVelocity();
with playerColliderCar.GetComponent().Increase$$anonymous$$axVelocity();
Also, you have not set the player's tag to "Player"
@ijuan I did just what you said, but now I have an Compiler Error: Assets/_Scripts/Coin.cs(29,4): error CS0103: The name `playerColliderCar' does not exist in the current context
It's playerCarCollider, my bad
I'd highly recommend you doing some C# Tutorials, seems like you are not understanding at all what your code is doing
Answer by tormentoarmagedoom · May 12, 2018 at 08:18 PM
I just explained how OncollisionEnter works here:
https://answers.unity.com/questions/1505361/objects-collide-but-oncollisionenter2d-not-called.html
Bye!
,,Is possibible you don't have the script containing the OnCollisionEnter2D inside one of the objects? It must.'' What do you mean?
Also I don't really understand the Example below. $$anonymous$$aybe you could visualize what you mean with all this?