gameobject

how to detect which gameobject is clicked

一笑奈何 提交于 2020-04-17 20:29:07
问题 I want to know which gameobject is clicked with mouse on a 2D project I used void Update() { if (Input.GetMouseButtonDown(0)) { clickTime = DateTime.Now; mousePosition = Input.mousePosition; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction); if (hit != null && hit.collider != null) { } } } but it never goes in the second if condition EDIT: I am working on a single script and access all gameobject from there using

How do I get an object to move and swap places with another object, on a mouse clic

点点圈 提交于 2020-01-24 10:44:27
问题 I have a script so far that moves an object a small distance upon a mouse click, however I want to change it so that when I click this object, it swaps places with another obejct next to it, instead of just the small distance it is moving now. I am a little confused on how to do this, because I am new to unity. using UnityEngine; using System.Collections; public class NewBehaviourScript: MonoBehaviour { public float movementSpeed = 10; void Update(){ if ( Input.GetMouseButtonDown(0)) {

User controlled Quaternion Slerp?

左心房为你撑大大i 提交于 2020-01-05 07:25:27
问题 I am trying to rotate a sphere with user control. void RotateGlobe() { SetLock(); if (Input.GetMouseButton(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); userInput.x = Input.GetAxis("Mouse X"); userInput.y = Input.GetAxis("Mouse Y"); if (userInput.magnitude > 0 & Physics.Raycast(ray, Mathf.Infinity, LayerMask.NameToLayer("UI"))) { Vector2 movement = new Vector2(Mathf.Sign(userInput.x), Mathf.Sign(userInput.y)); globe.transform.rotation = Quaternion.Slerp(globe.transform

Put UI image to Grid Layout Group Unity C#

与世无争的帅哥 提交于 2020-01-05 05:50:36
问题 I got trouble put the UI image inside Grid Layout Group in Unity. This What happen. Check the link below. [IMG] http://i65.tinypic.com/fp2dly.jpg [/IMG] [IMG] http://i65.tinypic.com/2iudlxe.jpg [/IMG] Whatever i have done change the Grid Layout Group Component : cell size value, spacing. It end with the same result like the url pic above with image gameobject cut at left side. The image gameobject size did not fix the content from the grid layout group. When i try to scroll it, it end with

Add Image component to new gameobject

独自空忆成欢 提交于 2019-12-31 05:27:49
问题 I created a new game object: GameObject newObject = new GameObject("ObjectName"); newObject.AddComponent<RectTransform>(); newObject.GetComponent<RectTransform>().sizeDelta = new Vector2(width, height); I'm looking for a way to add an image (script) for color purposes. How can I do it? 回答1: The same way you are adding RectTransform. newObject.AddComponent<Image>(); You also need to add using UnityEngine.UI to be able to find the Image component. 回答2: This adds the Image component to your

(Unity) How can I programmatically make Array Elements Clickable?

别等时光非礼了梦想. 提交于 2019-12-25 18:36:51
问题 I have Array Elements I got them on an Empty GameObject, I mean [SerializeField] and added through the script (C# Ofcourse), So the Objects are not really there they are being Generated when the Game begins. How can I clone the Collider from the Empty GameObject onto the clones in order to make them clickable? So far as of right now only the first one works witch is also the possition of the empty GameObject who has the colider on it now I need them onto the Clones as well... but,...How? I

How To Arrange a Desain Gameobject or UI to become Scrollable using Scroll Rect ? Unity C#

余生颓废 提交于 2019-12-25 18:23:07
问题 Please help me.. How to arrange gameobject or UI to become like the screenshot and make it scrollable using scroll rect ? The UI must be arrange like screenshot. There is a border yellow contain the UI and it must be scroollable. and then there is a cyan border contain 5 Slot item or more and scrollable too. it can be generate automatically or manual. And then there is a magenta border contain 3 button UI. Button 1, button 2, and button 3. Each button have different size. Why it must be

Unity - Refactored crumbling wall script stops working?

心已入冬 提交于 2019-12-19 10:58:33
问题 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;

Unity - Refactored crumbling wall script stops working?

我的未来我决定 提交于 2019-12-13 03:03:15
问题 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;

Unity 5 GameObject serialization

纵饮孤独 提交于 2019-12-12 21:57:48
问题 I am trying to serialize an entire GameObject with Newtonsoft.Json. When I serialize the object with JsonConvert it throws an error: NotSupportedException: rigidbody property has been deprecated UnityEngine.GameObject.get_rigidbody () (at C:/buildslave/unity/build/Runtime/Export/UnityEngineGameObject_Deprecated.cs:23) (wrapper dynamic-method) UnityEngine.GameObject.Getrigidbody (object) <IL 0x00006, 0x00073> Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue (object) (at Assets