OnCollision Enter not displaying a debug log - c#

using UnityEngine;
public class PlayerCollision : MonoBehaviour
{
void onCollisionEnter (){
Debug.Log("We hit something.");
}
}
I have rigidbody on every object and I attacked the script to my object, but still not working. I am following a Brackeys tutorial and it is a little old so I don't know if something has changed about the syntax.

void OnCollisionEnter(Collision collision) {
}

Related

Animation does not contain definition for 'play'

For no apparent reason Unity has started just now started showing errors in this code such as the attachment picture below.
I cannot get over this error and can't find a single explanation.
Since it seems fairly simple l think there might be some people who can give a hint.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Animation : MonoBehaviour
{
private Animation anim;
void Start()
{
anim = GetComponent<Animation>();
}
private void Update()
{
anim.Play();
}
}
Your own class is called Animation .. and no it has no such method ;)
Careful with naming your class equally to an already existing one.
You will now have to use explicitly UnityEngine.Animation everywhere
public class Animation : MonoBehaviour
{
private UnityEngine.Animation anim;
void Start()
{
anim = GetComponent<UnityEngine.Animation>();
}
private void Update()
{
anim.Play();
}
}
since otherwise the compiler will of course assume by Animation you are referring to your class itself.
In general: what is the purpose of calling Play every frame?

Find the 'Number' of the Scene the Player is in to Save it in PlayerPref Unity

I'm working on a 2D Pixel Platformer RPG, I need to develop a save and load mechanism in it. There are several scenes in the game (and will have many more), the question is, how do I save the scene number the player is currently in, so that when he quits and reloads the game, he's in the same scene. How can I implement it in C# Unity. (please be clear as I'm somewhat a beginner).
Ok, so there are a few things you need to do in order to achive this:
First, in the first scene in your build - create an Empty GameObject, name it "SceneManager".
Then, create a new tag "SceneManager" and add it to the "SceneManager" GameObject
Finally, add the "SceneManager" script to the "SceneManager" GameObject:
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneManager : MonoBehaviour
{
void Awake()
{
DontDestroyOnLoad(gameObject);
}
public void SaveScene()
{
int activeScene = SceneManager.GetActiveScene().buildIndex;
PlayerPrefs.SetInt("ActiveScene", activeScene);
}
public void LoadScene()
{
int activeScene = PlayerPrefs.GetInt("ActiveScene");
SceneManager.LoadScene(activeScene);
}
}
Then, you can load/save scenes by using this script:
using UnityEngine;
public class UsageScript: MonoBehaviour {
private SceneManager SceneManager;
void Awake ()
{
sceneManager = GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneManager>();
}
void UsageManager()
{
sceneManager.SaveScene();
sceneManager.LoadScene();
}
}

The local function 'OnTriggerEnter2D' is declared but never used

I don't know how to fix it. It's a 2D game. When you collide with the box, it should load/teleport you to the next scene, but it doesn't.
I tried everything on the internet and it didn't work.
using UnityEngine;
using UnityEngine.SceneManagement;
public class VictoryZone : MonoBehaviour
{
public void LoadNextLevel()
{
void OnTriggerEnter2D(Collider2D collider)
{
SceneManager.LoadScene(1);
}
}
}
I expect it to teleport me to my next level.
It looks like you nested the OnTriggerEnter2D() function inside of LoadNextLevel()? That's what the Local Function warning is referring to.
Those should be two separate functions, not one within the other. OnTriggerEnter2D() is a function of MonoBehavior; the MonoBehavior (VictoryZone) is what gets notified of the collision.
using UnityEngine.SceneManagement;
public class VictoryZone : MonoBehaviour {
public void LoadNextLevel() {
SceneManager.LoadScene(1);
}
void OnTriggerEnter2D(Collider2D collider) {
LoadNextLevel();
}
}
Note: you might also need to check the GameObject associated with collider to make sure that it's a player, and not an enemy or something (if, hypothetically, you had enemies or projectiles or other objects with colliders moving into the victory zone).

Show Canvas by trigger in Unity3d

I have a scene with two cubes, and canvas. One cube falls onto another. Canvas appearing must be triggered by collision. It's does not working. Where's the problem?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class EverythingInside : MonoBehaviour
{
public Canvas GUICanvas;
void Start()
{
}
void OnGUI()
{
GUICanvas.gameObject.SetActive(true);
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "FallingCube")
{
OnGUI();
}
}
void Update()
{
}
}
Put the code below inside your EverythingInside script. Make sure Collider and Rigidbody are attached to both cubes. Also make sure to drag your Canvas from the Editor to the GUICanvas slot.
public Canvas GUICanvas;
void Start()
{
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.CompareTag("FallingCube"))
{
GUICanvas.gameObject.SetActive(true);
}
}
void Update()
{
}
Unity Physics tutorials.
Unity UI Tutorials.
Unity Scripting Tutorials.
Your problem is that OnGUI() as a function is called once every frame in Unity, regardless of whether or not you call it manually. Also, OnGUI is reserved for doing things like rendering simple buttons and text directly to the screen, it doesn't perform any actual logic.
All you have to do to make this work is just replace where you've called OnGui() with GUICanvas.gameObject.SetActive(true)
and get rid of the other functions.
using UnityEngine;
using UnityEngine.UI;
public class EverythingInside : MonoBehaviour
{
public Canvas GUICanvas;
void OnCollisionEnter(Collision col)
{
if (col.tag == "FallingCube")
{
GUICanvas.gameObject.SetActive(true);
}
}
}
That right there is literally all you need to make it work. Bear in mind that if you change it later on to be a trigger instead of two colliding cubes you will need to replave OnCollisionEnter with OnTriggerEnter.

GetComponent upgrade from 4 to 5 not work

In my Unity 4.3 all work well, but after Upgrade to 5 I have a problem with GetComponent. To test a new deprecable GetComponent I have use the official tutorial
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
public GameObject otherGameObject;
private AnotherScript anotherScript;
void Awake ()
{
anotherScript = GetComponent<AnotherScript>();
}
void Update ()
{
Debug.Log("The player's score is " + anotherScript.playerScore);
}
}
And the second script
using UnityEngine;
using System.Collections;
public class AnotherScript : MonoBehaviour {
public int playerScore = 9001;
}
This is only for test,
i've used the same example of the unity tutorial
https://unity3d.com/learn/tutorials/modules/beginner/scripting/getcomponent
After that I've associated the two object in the editor But the run report is:
NullReferenceException: Object reference not set to an instance of an object
test.Update () (at Assets/test.cs:22)
in unity 4.3 work well.
You should try getting the reference in the Start method. Make sure that both test and AnotherScript scripts are attached to the-same GameObject in the Editor, then the code below should work.
using UnityEngine;
using System.Collections;
public class test: MonoBehaviour
{
public int playerScore;
void Start()
{
anotherScript = gameObject.GetComponent<AnotherScript>();
}
void Update ()
{
Debug.Log("The player's score is " + anotherScript.playerScore);
}
}
If both Scripts are not attached to the-same GameObject then use:
anotherScript = GameObject.Find("Name of GameObject AnotherScript is Attched To").GetComponent<AnotherScript>();
You need to use FindObjectOfType<AnotherScript>() if the scripts are not attached to the same GameObject.
I believe this should be so:
public GameObject otherGameObject;
private AnotherScript anotherScript;
void Awake ()
{
anotherScript = otherGameObject.GetComponent<AnotherScript>();
}
void Update ()
{
Debug.Log("The player's score is " + anotherScript.playerScore);
}
}
And you should attach otherGameObject in the editor.
Are you sure that AnotherScript is also attached to the same Gameobject you test-script is attached to?
The GetComponent Method only looks for Components attached to GameObjects.
The easy way would for sure be to make anotherScript member public (so it´s exposed in the inspector) and drag´n´drop your script in there to get the reference.

Categories

Resources