FAQ Database Discussion Community


Changing sprites using Sprite.Create does not take effect?

unity3d,sprite,unityscript,gameobject,unity3d-2dtools
I'm making minecraft-like 2D game and everything seems like it's okay except changing block texture in one chunk. This is how I try to change sprite: var block = new GameObject("blok", typeof(SpriteRenderer), typeof(BoxCollider2D), typeof(CircleCollider2D)); block.GetComponent(SpriteRenderer).sprite = Sprite.Create(Resources.Load("/Prefabs/Blokovi/blok_zemlja") as Texture2D, Rect(0, 240, 16, 16), Vector2(0.5, 0.5), 16); It does not take...

Create array/list of many objects(initially unknown amount) by tag

c#,arrays,list,unity3d,gameobject
I'm currently working on a radar system for my space game, and I am trying to work out how to add gameobjects, by tag, to either a list or array that can then be used in other methods. I can't do this manually because I will be procedurally generating each...

GameObject.FindObjectOfType<>() vs GetComponent<>()

c#,unity3d,gameobject
I have been following several tutorial series and have seen these two used in very similar ways, and was hoping someone could explain how they differ and possibly examples of when you would use one instead of the other (presuming that they are actually similar!). private LevelManager levelManager; void Start...

adding GameObject at runtime

c#,unity3d,gameobject,unity3d-2dtools
This may seem like a stupid question but I'm stuck with it. I have GameObjects in a list (List<GameObject>) and I want to add them on the scene runtime, prefarbly on predefined places (like placeholders or something). What would be a good way to do it? I've been searching the...

Unity - Overriding GameObject's position attribute

c#,unity3d,overriding,gameobject
I have a complex GameObject (i.e. it is a parent to many other game objects) and I want to override its position attribute. For example take the following game object ParentGameObject-Child1-Child2 Now I want to position ParentGameObject with respect to the position of Child2, i.e. when I set ParentGameObject.transform.position it...

unity | Find child gameObject with specified parent

unity3d,find,parent-child,gameobject
I have to find child gameObject with specified parent. Uhm. Let me explain more detail. I'm creating the prefab which have one child gameObject named 'state' and the name of parent gameObject is changed when i play the game. I mean the name of child gameObject is always same but...

Save prefabs to array

javascript,arrays,unity3d,gameobject
In my game, players shall be able to select units from a menu, which will later be used (placed) in various scenes. For that, I want to save the unit prefabs in a static array through code. I then want to access these prefabs, to display some of their variables...

NullReferenceException in FindGameObjectWithTag

c#,unity3d,gameobject
I've a panel in my scene (ErrorMessage), I've been disabled it in the editor and writed this in my C# script: if(getUsernameResponse == "Login OK") { Application.LoadLevel("LobbyUI"); } else { GameObject ErrorMessage = GameObject.FindGameObjectWithTag("ErrorMessage"); ErrorMessage.SetActive(true); } The script should enable (show) my ErrorMessage if getUsernameResponse have a different response of...

Unity: An object reference is required to access non-static member - Ray cast to GameObject

c#,unity3d,raycasting,gameobject,playmaker
I'm making an RTS style game and i've got an error. I'm trying to send the current selected unit (the object the script is on) to the Playmaker FSM of the object the raycast hit. I realised that you cannot access gameobjects and transforms inside of a static function so...

Unity - move 3d object in an AR scene

unity3d,3d,augmented-reality,movement,gameobject
I have an AR scene that has one AR camera, an image target and 3d object below as. I create an .cs file and attach to ARCamera. I want to move AR object to mouse click position. I tried many codes for this. But I couldn't success it. I know...

Right way to instantiate GameObject?

c#,unity3d,instantiation,gameobject
So I have a dilemma about right way to instantiate prefab gameObject in Unity c#. Here is the 2 example codes Example 1: public Transform musicPrefab; GameObject mManager = (GameObject)Instantiate(musicPrefab,transform.position, Quaternion.identity); Example 2: public Transform musicPrefab; Object mManager = Instantiate(musicPrefab,transform.position, Quaternion.identity); But instantiating as Object works as well, so which...

Duplicating prefabs and gameobjects

unity3d,gameobject
I have a model of a house which I want to repeat many times. If I make that house as a prefab and duplicate that prefab then will it be counted as mesh or its just a clone of prefab and will not be counted as mesh and there will...

Multiple instances of gameObject not performing identically

c#,unity3d,gameobject
So I have three different 'bystanders' in my game world, each with an attached 'Bystander' script, and have a 'BystanderDialogue' element of my UI. The idea is that when the player comes in range of ANY of the bystanders, randomly selected text from a database is displayed, but for my...

Unity: Prefab parenting in code

unity3d,gameobject
Let's say I want multiple prefab object called childTile, it parenting another single prefab object called parentTile. So whenever the parentTile rotates, childTiles will be rotated around parentTile. Basically this is what I wrote: public GameObject childPrefab; public GameObject parentPrefab; void Update() { for(int i = 0; i < 10;...

How to make a gameObject invisible?

unity3d,gameobject
I am attempting to make a Unity gameObject invisible through code. So far I have tried (On a script attached to the gameObject I want to make invisible): this.renderer.enabled = false this.enabled = false this.gameObject.enabled = false this.gameObject.renderer.enabled = false And on my player script: other.gameObject.enabled = false other.gameObject.renderer.enabled =...

How to add to scene own gameobject on unity

unity3d,gameobject
I create my own cube. I use below code to do it void Start () { MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>(); Mesh mesh = new Mesh (); meshFilter.mesh = mesh; mesh.vertices = new Vector3[]{ // face 1 (xy plane, z=0) new Vector3(0,0,0), new Vector3(1,0,0), new Vector3(1,1,0), new Vector3(0,1,0), // face 2...