• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Philip Clevberger · Dec 04, 2010 at 01:22 PM · helloworld

Can I get an example of "Hello World" in Unity C#

Hi, I'm new to Unity 3D, My background is in Flash Actionscript3, so when I have been looking around for tutorials, it feels a bit like old skool flash where you attache snippets of code to visual objects on the screen. So my questions are.

  1. Is it possible to have something like a document class, a piece of code that runs first, where I can instantiate objects. Place things on screen etc. etc. As supposed to dragging and dropping things and placing things visually?

  2. C# seems neat so that's he most tempting way to go. I haven't been able to find any "getting started" tutorials. I would like something like HelloWorld, and maybe something where you get to set up the environment and throw a few cubes on the screen and get them to spin using code.

Thanks in advance /Philip

Comment
duck
RDC

People who like this

2 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sacredgeometry · Dec 04, 2010 at 11:48 PM 0
Share

you can use a null objet as that document class if you want, just run as script and stick the code you want to load on launch inside the awake or start methods. ie

void Start() { // code you want to launch here }

1 Reply

  • Sort: 
avatar image

Answer by duck · Dec 04, 2010 at 01:41 PM

1) No, you have to have at least one script attached to a GameObject in your scene. This can be an empty gameobject (i.e. a non-visible item), but there's no scene root node, so you need this.

The script's "Awake" or "Start" functions can then be used to create and trigger other classes and functions. There's no "scene root node".

2) Here's a few of c# examples to make an object say hello, move, rotate, or be clickable in the scene:

// HelloWorld.cs (outputs a line of text to the console) using UnityEngine; public class HelloWorld : MonoBehaviour {

 void Start() {
     Debug.Log("Hello World!");
 }

}


// RotateMe.cs (rotates the object at a specified speed) using UnityEngine; public class RotateMe : MonoBehaviour {

 public float rotateSpeed = 10;
 void Update() {
     transform.Rotate( rotateSpeed * Time.deltaTime, 0, 0 );
 }

}


// MoveMe.cs (moves the object via the arrow keys) using UnityEngine;

public class MoveMe : MonoBehaviour {

 public float moveSpeed = 2;

 void Update() {
     float moveX = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
     float moveZ = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
     transform.Translate( moveX, 0, moveZ );
 }

}


// ClickHello.cs (outputs a line of text to the console when the object is clicked) using UnityEngine; public class ClickHello : MonoBehaviour {

 void OnMouseUp() {
     Debug.Log("Hello World! "+name+" was clicked!");
 }

}

These scripts show a few fundamental aspects of coding in Unity:

  • Public variables are exposed in the inspector
    This means you can adjust the variable's value for each object independently which uses this script, using Unity's inspector window

  • Framerate-indepenedent movement using Time.deltaTime This allows you to move an object at a constant speed regardless of the framerate currently being achieved by the computer. This avoids problems such as cars driving fater on higher-spec machines!

  • The Start and Update functions These are examples of built-in unity events which are sent by the engine. Start is called when an object comes into existence, and Update is called every frame. There are many other event functions which are sent to any scripts attached to Game Objects. For the full list, see the MonoBehaviour reference.

  • Direct references to common components, such as transform Because your script inherits from MonoBehaviour (by default), you inherit these properties which are useful aliases to common types of component that get attached to gameobjects such as "transform", "collider", "renderer" "light", "camera", etc. For the full list, see the Component reference.

  • Input class
    contains the API for all types of input including keypresses, buttons, custom axes, iphone touches, etc.

For more general information about getting started learning unity, see:

How can I start learning Unity fast?

Good luck!

Comment
Peter G
Loius
Jesse Anders
BinaryCaveman
almo
nuno
Rioneer
Kiwasi
7a
playa245
Guigondi

People who like this

11 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aalstes · Jul 23, 2013 at 03:17 PM 0
Share

This is just what I've been looking for! Thanks! :)

avatar image farmcp · Jul 13, 2014 at 11:44 PM 0
Share

Where would I be able to see the Debug.Log("hello world");?

avatar image Bunny83 · Jul 13, 2014 at 11:55 PM 0
Share

@farmcp:
In the Console window

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Is there anyway to only show a part of a GUI image during the game? (C#) 1 Answer

Is the scripting API source code available? 2 Answers

Problem with script 1 Answer

Difference between if statement, while and for loop? 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges