gameobject

Unity - How to get GameObjects from a different scene?

血红的双手。 提交于 2021-02-17 06:32:47
问题 first of all, I'm a beginner so go easy on me. I'm trying to activate and deactivate GameObjects (players in the game) from another scene. Basically, I'm trying to change the player in the game scene by deactivate the current player model and activate the wanted player model in the game scene when the user press certain button in the shop scene, but i cant access the GameObjects (players in game scene) in the game scene from the shop scene. Does anyone know what can I do? 回答1: You will have

Unity - How to get GameObjects from a different scene?

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-17 06:32:10
问题 first of all, I'm a beginner so go easy on me. I'm trying to activate and deactivate GameObjects (players in the game) from another scene. Basically, I'm trying to change the player in the game scene by deactivate the current player model and activate the wanted player model in the game scene when the user press certain button in the shop scene, but i cant access the GameObjects (players in game scene) in the game scene from the shop scene. Does anyone know what can I do? 回答1: You will have

Accessing a script from another script at runtime in Unity C#

走远了吗. 提交于 2021-02-11 14:02:31
问题 Like in here, but the difference is that it's supposed to be done from an instantiated prefab, so I can not drag the GameObject, that has the script with the variable I want to access, into this script. This was working public ScriptA script; void Update() { if (script.varX < 0) { // . . . } } But now I'm getting "Object reference not set to an instance of an object" error, which I think comes from the fact that the script trying to access ScriptA, is attached to an instantiated prefab. How

How to create game-wide object in Unity3d without singleton?

偶尔善良 提交于 2021-02-10 14:16:25
问题 Is there a way in Unity3d framework to register an object that would be accessible to all entities in all scenes in the game without resorting to singleton pattern (like a game state object)? 回答1: You could take the following approach: In the scene where the GameObject is created, in a MonoBehavior script attached to the GameObject: Name the GameObject with "name = ..." in Awake() (or in the Editor) Example: myObject.name = "FindMeInEveryScene"; Ref: https://docs.unity3d.com/ScriptReference

How can I grab a reference of a game-object in a scene from a prefab

旧街凉风 提交于 2021-02-05 08:48:09
问题 currently I am working on a Equipment system that will Instantiate Game objects into the scene from a list, prior to this I was not using a Instantiation system with just keeping the Game Objects Active in the scene at all time. Now with Instantiating the Game Object(Prefab) I am running into the error of Loosing the References when I instantiate them, I have looked at various other articles before such as this Discussion here: However I am looking at alternative ways other than using Tags as

why GetComponent returns null reference for imageEffect?

五迷三道 提交于 2021-02-05 08:16:46
问题 this problem is unsolvable or what?i don't need to provide any code example it just doesn't work to anyone!and i don't know how its possible,you just have to try this : GetComponent<DepthOfField>().enabled = false; assume that we have a MainCamera object and we attach it a DepthOfField image effect Script.and we create another script named whatever and attach it to the MainCamera and in the whatever script we just call this: void Start() { GetComponent<DepthOfField>().enabled = false; }

Hide/Unhide object in Unity3D via script

…衆ロ難τιáo~ 提交于 2021-01-28 00:15:00
问题 i want to set part of my scene to be hidden before everything starts, so lets say i have some isHidden (which im trying to find) property that i set to true before, or at the first frame of my scene. at some point i want to unhide it and trigger the animation, i know how to trigger the animation. and the only way i found to hid object is to set it like : SetActive(false); it does hide is, but when i am trying to access it later in the script with GameObject.Find("gameObjectName"); it returns

An error occurs when trying to set a child to my GameObject in script

家住魔仙堡 提交于 2020-06-27 10:45:27
问题 GameObject enemy = Instantiate(spawnObject,spawnPosition,spawnObject.transform.rotation) as GameObject; enemy.transform.parent = transform; The above code generates the expected result when I test my game in game mode, however I'm gettting this error message: "Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption." Yes, the spawnObject variable contains a prefab, however creating a new GameObject should have fixed the problem, I assume? 回答1: Check

An error occurs when trying to set a child to my GameObject in script

无人久伴 提交于 2020-06-27 10:45:00
问题 GameObject enemy = Instantiate(spawnObject,spawnPosition,spawnObject.transform.rotation) as GameObject; enemy.transform.parent = transform; The above code generates the expected result when I test my game in game mode, however I'm gettting this error message: "Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption." Yes, the spawnObject variable contains a prefab, however creating a new GameObject should have fixed the problem, I assume? 回答1: Check

how to detect which gameobject is clicked

爷,独闯天下 提交于 2020-04-17 20:30:48
问题 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