When my game in unity starts, my character collides with objects it isn't touching - c#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pickup : MonoBehaviour
{
public GameObject _Pickaxe;
// Start is called before the first frame update
void Start()
{
_Pickaxe = GameObject.Find("Pickaxe");
}
// Update is called once per frame
void Update()
{
}
void DestroyGameObject()
{
Destroy(gameObject);
}
private void OnTriggerEnter(Collider collision)
{
Debug.Log("Collided with +" + collision.gameObject.name);
if(collision.gameObject.name == "Pickaxe" );
{
Debug.Log("Touched pick");
}
}
}
In my script it logs whatever the character touches. In my scene i have a object placed about 10m away from where the player capsule spawns, somehow it collides with it before you can even move.

Your code have an error. It logs "Touched pick" for every collision. To fix it, your should remove the semicolon in this line if(collision.gameObject.name == "Pickaxe" );

Related

destroying gameObject in both cases in Unity

I have written a code where when the player is not being rendered in camera it should be destroyed but it is being destroyed even being rendered in camera, please see my below code;
using UnityEngine;
public class IfnotvisibleDestroy : MonoBehaviour
{
public SpriteRenderer re;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
public void Update()
{
if (re.isVisible)
{
Debug.Log(re.isVisible);
}
if(!re.isVisible)
{
Destroy(gameObject);
}
}
}
Do the destroy gameObject part inside the FixedUpdate function

How can I destroy a game object forever?

So, I have this potion. When the player in my game comes in contact with the potion, I want to destroy the potion. However, if the player dies, the scene will reload and the potion will still be in the level. If the player collides with the potion, I don't want them to obtain it. They should only be able to collect the potion once.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyPotionForever : MonoBehaviour
{
public bool potionCollide = false;
// Start is called before the first frame update
void Start()
{
}
void OnTriggerEnter(){
if(potionCollide == false){
Destroy(gameObject);
bool potionCollide = true;
}
}
// Update is called once per frame
void Update()
{
}
}
But...this code doesn't work. Any help is appreciated.
A simple way would be to store whether you had picked up the potion in PlayerPrefs.
Then you could do something like:
void OnTriggerEnter()
{
if(PlayerPrefs.GetInt("GotPotion", 0) == 0)
{
// You didn't get the potion yet, so get it
Destroy(gameObject);
PlayerPrefs.SetInt("GotPotion", 1); // got the potion
}
}
Then wherever you spawn your potion, you could have:
if(PlayerPrefs.GetInt("GotPotion", 0) == 1)
{
// Got the potion already, so don't spawn the potion
}
Or, you if you put the potion in the scene directly, you could do:
void Start()
{
if(PlayerPrefs.GetInt("GotPotion", 0) == 1)
{
// If the potion is already picked up, destroy it
Destroy(gameObject);
}
}
A much better way would be to write your own save system as Antnio Pedro Gonalves Ferreira suggested, but this will get you through the demo phase at least.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyPotionForever : MonoBehaviour
{
public bool potionCollide = false;
// Start is called before the first frame update
void Start()
{
}
void OnTriggerEnter(){
if(potionCollide == false){
Destroy(gameObject);
potionCollide = true;
}
}
// Update is called once per frame
void Update()
{
}
}
Just using global var. potionCollide . You created local variable instead of using variable global.
Is your potion on the scene before you start the game? Whatever happens during runtime does not permanently change the scene, if you want the potion to disappear forever it cannot be on the hierarchy of the scene before the game runs. When you reload a scene it resets to the state it was before you ran the game.

How to transform position from one object to the current player position?

So i have this little particlesystem attached on my player so if he dies he explodes. But i cant simply attach the particlesystem under the player because if i destroy my player the childs of the gameobjects get destroyed as well. The animation runs if he dies but not on his current spot so some ideas for that? Maybe to transform the position to the current position of the player while he dies?
Here my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerdeath : MonoBehaviour
{
public ParticleSystem death_explosion;
// Start is called before the first frame update
void Start()
{
death_explosion.Stop();
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "deathcube")
Destroy(gameObject);
Debug.Log("collision detected");
death_explosion.Play();
}
}
What you could do is create a prefab of the particle object and reference it inside your script like so:
public ParticleSystem death_explosion_Prefab;
And instead of attaching it to the Player as a child, instantiate it on collision:
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "deathcube")
{
Debug.Log("collision detected");
Instantiate(death_explosion_Prefab, gameObject.transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
got it :) --> add death_explosion.transform.position = GameObject.Find("player").transform.position;

OnCollisionEnter not detecting

I am trying to make the game go to Dead scene when collided by does not work. And it even does not detect the collsion.
I've attached the script to empty game object that has box collider with is trigger ticked and the player does have a Rigidbody too.
I am not sure what is wrong, please help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Health : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void OnCollisionEnter(Collision collision)
{
Debug.Log("dead without if statement");
if (collision.gameObject.tag == "Player")
{
Debug.Log("Dead Mate");
SceneManager.LoadScene("DeadScreen");
}
}
}
"I've attached the script to empty game object that has box collider with is trigger ticked and the player does have a Rigidbody too."
If i understand you correct you enabled trigger on box collider if yes you must implement
void OnTriggerEnter(Collider col)
{
}
Not
public void OnCollisionEnter(Collision collision)
{
}

Unity Collision Detection - Adding GUI Score on Collision?

I am making a pinball game in Unity, and I have an issue. When the pinball collides with a cylinder to add points to the score, it does not work. I have tagged the cylinders in Unity and have attached this script to the pinball. It doesn't even show up in the debug log.
Thanks for any advice.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Score : MonoBehaviour {
public int scorePoint = 10;
public int MaxScore;
public Text ScoreText;
// Use this for initialization
void Start () {
ScoreText = GetComponent<Text>();
ScoreText.text = "Score: " + scorePoint;
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "Cylinder")
{
Debug.Log("Collision detected");
scorePoint+=10;
}
}
// Update is called once per frame
void Update()
{
}
}
Make sure you have a box collider on each object. OnTriggerEnter is only called when two box collider hit each other. This is the most likely culprit of why its not working but without more information I can't guarantee it.

Categories