Simple log in unity c# script - c#

It should be simple, but still can't find a straight answer:
How can I log a simple message using C# Unity script?
I tried this:
Debug.Log("Hello Log");
It doesn't work or I'm not looking at the right place.

Debug.Log("Hello Log"); can be found on the Console Tab.
For you to be able to see it:
1.You must put it in a function from a script.
public class Test : MonoBehaviour
{
void Start()
{
Debug.Log("Hello Log");
}
}
2.Script must be attached to a GameObject.
3.Both the script and the GameObject it is attached to must be enabled.
4.Reset the Layout if you can't see the Console tab.
EDIT:
I tested it on Android Device. That's why I didn't see it.
In that case, use Android Monitor from Android Studio to view the log from your Android device.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Simple : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("Attach this script to component and simple message will be there when you start project");
}
// Update is called once per frame
void Update()
{
}
}

Related

I'm having an error in unity for a script that changes the scene to another scene

This is an image of the errors
This is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManager;
public class LevelComplete : MonoBehaviour
{
public void LoadNextLevel ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().BuildIndex + 1);
}
}
Please help me with the errors
I tried to debug the error by checking the script and UI animations that connect to the event that changes the scene. I also tried using SceneManager.LoadScene (name); but it did not work.
You have two issues here. First of all, you declared the class twice. It might be twice in the same file, or in different files. Second of all, you need to use UnityEngine.SceneManagement instead of SceneManager.
The simplest solution is to check for double declarations, and for the using, replace
using UnityEngine.SceneManager; with using UnityEngine.SceneManagement.

Problem In Animation On Brackeys Tutorial

I've been following Brackeys's tutorial on how to make a video game in Unity3D. Up to now things have been great.
But now when I create a game over screen and animated it, it keeps on looping when it triggers. I've tried turning off loop time and transitioning to an empty state with no exit time. It also doesn't loop the whole thing just around less than half a second.
My EndTrigger script and CompleteLevel script is below. I don't get any debugging errors.
using unityEngine;
public class EndTrigger : MonoBehaviour {
public GameManager gameManager;
void OnTriggerEnter ()
{
gameManager.CompleteLevel();
}
}
My CompleteLevel Script Below
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelComplete : MonoBehaviour {
public void LoadNextLevel ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
In your project files select the animation file and make sure you've turned off Loop Time. Unity enables it by default.

Unity giving compile error without any error

I am learning unity 3d game development and I was following a tutorial and wrote this code for a ball to apply force on its X-axis, but unity said all compile errors must be resolved before going in Playmode:
code:
using System.Numerics;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddConstantForce : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
GetComponent<RigidBody>().velocity = new Vector3(2,0,0);
}
}
I have update unity version. tried various new sample scenes, and after this error pops it also gives the error with default empty void start and void update functions too.
please help
Without the error information, this is somewhat difficult to debug but I do see two errors with your code.
First, you have a typo on your Update function you are calling GetComponent<RigidBody> when the actual name of the class is Rigidbody see Unity's documentation for reference https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html
There is also an ambiguous reference between Vector3 as both Systems.Numerics & UnityEngine defines that type. you want to stick with the UnityEngine definition. so just delete the first line.
So in the end this code should be working without compilation errors
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddConstantForce : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
GetComponent<Rigidbody>().velocity = new Vector3(2,0,0);
}
}
Try define the getcomponet variable in the void update and then make a new line with the varible first then velocity thing or/and use += insted of =.

How to: Correctly reference a Text UI component in a script

Working on getting familiar with C# and unity development. Today I am working on getting a reference to a Text UI object in my script. The following code below produces this error:
NullReferenceException: Object reference not set to an instance of an object
handle.Awake () (at Assets/handle.cs:20)
Script looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using playPORTAL.Profile;
using UnityEngine.UI;
public class handle : MonoBehaviour
{
public Text myText;
// Start is called before the first frame update
void Start()
{
}
void Awake()
{
myText.text = "#organickoala718" ;
}
// Update is called once per frame
void Update()
{
}
}
What needs improving to correctly get a reference to the Text UI element?
In general: The same way as with any other Component.
Either reference it via the Inspector or use GetComponent(Tutorial) or one of its variations.
So if that Text Component is attached to the same GameObject as your script then you could use GetComponent(API) to get the reference on runtime
private void Awake ()
{
if(!myText) myText = GetComponent<Text>();
myText.text = "#organickoala718" ;
}
Also checkout Controlling GameObjects with Components
Btw you should remove the empty methods Start and Update entirely. They are called by the Unity Engine as messages if they exist so the don't need to exist and only cause unnecessary overhead.
You need to either set the myText value of your handle instance from another script, or set it in the Unity Editor's Inspector window, when you've selected a GameObject that has your handle component added.
You need to drag the object you are referencing to from the Unity editor, in the scene, to the script itself.
First attach the script you made to a GameObject in the Unity Scene.
Then drag the "text" component to the Script you recently attached to the GameObject
That would solve the problem you have.
Another approach would be to declare a
public GameObject UITextElement;
Instead of a public text as you did.
Do the same as i wrote before and in the script write:
UITextElement.GetComponent().text = "Write your text here!";

Unity Script is not running

i am creating all new unity project and added Empty GameObject in to my first Scene, and also created and added script to that object, but when i'm trying to run my scene script dose not showing any output in my console window.
here is my code
using System.Collections;
using UnityEngine;
public class ScriptObject : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("Start method called.");
}
// Update is called once per frame
void Update()
{
Debug.Log("Update method calling.");
}
}
You need to turn on showing of another types of log messages ("Info" messages in your case).
You need to be sure that your button of the console output is enabled. Just try to click on the button.Look at the screenshot

Categories

Resources