gameobject

Unity/C# Find object and get component

强颜欢笑 提交于 2019-12-10 16:01:42
问题 This should be an easy one: GameObject myCube = GameObject.Find("Cubey").GetComponent<GameObject>(); just kicks up error CS0309: The type UnityEngine.GameObject must be convertible to UnityEngine.Component in order to use it as parameter T in the generic type or method UnityEngine.GameObject.GetComponent() Normally the errors Unity displays are useful, but this is just confusing. Are cubes not GameObjects? Any pointers would be appreciated (no pun intended). 回答1: An easy mistake, been there..

Unity add child to children, but at top

随声附和 提交于 2019-12-10 12:53:27
问题 I am trying to add a child object to a collection of children, but I want to make sure the the latest will be the first. Here is what I am trying to do: GameObject - (My new object here) - GameObject - GameObject - GameObject Here is the code I am using to instantiate my prefab: GameObject messageObj = Instantiate(storyPrefab) as GameObject; messageObj.name = "Story"; messageObj.transform.parent = wallGrid.transform; messageObj.transform.localScale = new Vector3(1,1,1); Hope this makes sense.

How can I find child gameobject?

天涯浪子 提交于 2019-12-05 16:29:37
问题 I want to say somethings like.. Gameobject.find(child gameobject of specific parent Gameobject) Can anyone help me. thanks! 回答1: GameObject.Find will search for a gameobject in the scene. To search a gameobject from a parent, use Transform . There are 2 ways of doing it: transform.Find("childname") transform.FindChild("childname") The 2nd option is deprecated but still functional, so you'd better use the 1st option. 回答2: Fixing Jay Kazama's answer. The correct answers are: transform.Find (

How can I find child gameobject?

别等时光非礼了梦想. 提交于 2019-12-04 00:13:25
I want to say somethings like.. Gameobject.find(child gameobject of specific parent Gameobject) Can anyone help me. thanks! Jay Kazama GameObject.Find will search for a gameobject in the scene. To search a gameobject from a parent, use Transform . There are 2 ways of doing it: transform.Find("childname") transform.FindChild("childname") The 2nd option is deprecated but still functional, so you'd better use the 1st option. Fixing Jay Kazama's answer. The correct answers are: transform.Find ("childname") transform.FindChild ("childname") With small t (property transform, not class Transform). If

Clone/duplicate an existing GameObject and its children

℡╲_俬逩灬. 提交于 2019-12-02 11:55:03
问题 Is there a C# way in Unity to duplicate an existing GameObject and all of its children? In my case, I have an empty GameObject with a number of Text objects as children and I would like to create a copy of them all, including relative positions, text values, font, colors, etc.... Prefabs won't work easily because I want to copy the object including its current state. 回答1: The Instantiate function is used to clone any GameObject and its hierarchy. public GameObject rootObj; void Start() {

Moving an object with vector3.MoveTowards

泪湿孤枕 提交于 2019-12-02 05:21:41
问题 I am working on a game app with Unity. I have an issue when it comes to move a GameObject . In my game, when the player swipes up with his device, the GameObject moves from a point A to B, and when he swipes down, it goes from B to A. I wrote a C# script with the game logic, but I have an issue when it comes to this. The problem is that the GameObject moves instantly from A to B. Here is the code line I use to move my GameObject : transform.localPosition = Vector3.MoveTowards (PositionA

Moving an object with vector3.MoveTowards

让人想犯罪 __ 提交于 2019-12-02 03:21:27
I am working on a game app with Unity. I have an issue when it comes to move a GameObject . In my game, when the player swipes up with his device, the GameObject moves from a point A to B, and when he swipes down, it goes from B to A. I wrote a C# script with the game logic, but I have an issue when it comes to this. The problem is that the GameObject moves instantly from A to B. Here is the code line I use to move my GameObject : transform.localPosition = Vector3.MoveTowards (PositionA,PositionB,Time.deltaTime * speed); speed is a float with a value of 10.0f. I would like my GameObject to

Unity - Refactored crumbling wall script stops working?

隐身守侯 提交于 2019-12-01 12:18:05
I have an Object that gets replaced by thousands of little cubes at once, which then begin moving one after another after initialization. I have code that works, but when I try to refactor it to clean it up, it stops working. The cubes dont move. This happens when I try to separate the Variable Initialisation and the Movement Initialisation. So this is my original code segment and it works: public class WallCreation : MonoBehaviour { public Transform wallSegmentPrefab; GameObject oldWall; Vector3 oldWallSize; int oldWallsizeX; int oldWallsizeY; int oldWallsizeZ; Vector3 oldWallPosition;

Add gameobject dynamically to scene in Unity3d

狂风中的少年 提交于 2019-11-29 06:07:41
问题 I am creating a scene in which I want to show list of offers. In order to show the offer, I created a prefab with placeholders for the offer details which I will get at runtime. I created a place holder in the scene to add the prefab to the scene, but it is not showing on the UI. OfferHolderClass: using UnityEngine; using System.Collections; public class OfferHolder : MonoBehaviour { public GameObject localOffer; // Use this for initialization void Start () { GameObject offer = Instantiate

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

戏子无情 提交于 2019-11-27 01:25:11
问题 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, if possible, examples of when you would use one instead of the other (presuming that they are actually similar!). private LevelManager levelManager; void Start () { levelManager = GameObject.FindObjectOfType<LevelManager>(); } and private LevelManager levelManager; void Start () { levelManager = GetComponent<LevelManager>(); } 回答1: You don