- Home /
How can I create object using class?
Hello. I am new to Unity and i want to create a prefab object using class. Unfortunetly i dont know how to spawn my prefabricated ball on the scene. Thats my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KontrolerGracza : MonoBehaviour {
public class Gracz
{
public int movementSpeed ;
public int jumpPower;
public bool jumping;
}
public void Spawn()
{
Gracz player = new Gracz();
player.movementSpeed = 10;
player.jumpPower = 900;
player.jumping = false;
}
void Start()
{
Spawn();
}
}
There's a code formatting tool (little 0s and 1s on the text entry pane). Fixing it for you this time.
After formatting, that probably doesn't compile unless that's a nested class. Can you post the entire script to confirm?
Yep ty, so you want that object to have a representation in your scene?
Answer by hectorux · Nov 06, 2018 at 05:39 PM
There is a method called Instantiate(object) (Have more "I dont know the word now" overrides). If you set it into a prefab and assign it in the inspector will work fine. Also there exists AddComponent() wich will set into the object your script class
Your answer
Follow this Question
Related Questions
Spawning different items from different prefabs 2 Answers
Spawning objects around a point 1 Answer
Different way to spawn prefabs 0 Answers
Spawning Coins infront of the player 2 Answers
Resource Load Prefab 0 Answers