In unity why cannot Instatiate a gameobject - unity3d

I am currently coding in Unity and I am trying to Instantiate a gameObject. The mechanism I'm developing will delete the Image I'm on and load the image underneath, using two buttons ; One to create and one to delete thus giving the effect of moving through a graphical stack.
I have thought about moving the image forward along the z axis so it is out of view, but I have read that the object MUST be instatiated and not Transformed
I have a Rigidbody2d and (2x) 3D Box Collider over the image that has the code. One on the left and one on the right
The delete button works a treat and just to check I do not have a problem with the box collider I have tried the delete code on the other button which also works fine, so I have come to the conclusion that the code is flawed in some respect
My problem is that I cannot seem to recreate the object once it has been deleted, it is my understanding that the object does not have to exist in world space for it to be recreated. If I use the Quaternion Identity I get a few errors about the gameObject being rendered off screen
***
Instantiate(gameObject,new Vector3 (8,-17,11),
// Removed until I can actually get the image to Instantiate
Quaternion.identity);
***
I have also tried
***
Instantiate(gameObject,new Vector (gameObject.transform.position.x -0.08, gameObject.transform.position.y, -0.17, gameObject.transform.position.z, 11), Quaternion.identity);
}
}
***
Can someone please tell me what I am doing wrong?

I am fairly new to Unity too, but I believe what you need is a Button that Swaps the Sprite image.
Select your Button in the Hierarchy.
In the Inspector:
In the Image component, drag in the first Sprite
In the Button Component:
select the check box Interactable
select Sprite Swap in the Transition Drop Down
drag your second Sprite to "Selected Sprite"

I am fairly new to Unity too, but I believe what you need is a Button that Swaps the Sprite image.
Select your Button in the Hierarchy.
In the Inspector:
In the Image component, drag in the first Sprite
In the Button Component:
select the check box Interactable
select Sprite Swap in the Transition Drop Down
drag your second Sprite to "Selected Sprite"

Related

how to change hierarchy on prefabs

I am new to unity and working on a image viewer in unity2D.
I am not sure if I choose components true but for now working fine.
most of the project is script based
I created a prefab which has a raw image
and in script I Instantiate prefabs in a scrollviews content
images are displayed no problem in that
I added some events where I Instantiate my prefab pointerDown, beginDrag, endDrag
dragging workin fine too
but when I drag any added prefab to older added prefab it stays on top but if I drag it to a newer added prefabs it stays under them.
I think its because the hierarchy order.
how can I change the order or how can I manage to make prefab stay on top when dragging
ps: on the image thumb_0 is added first and after drag it stays on behind thumb_2
if you can access the Transform of your prefabs you can use the method SetAsLastSibling(); it will move your gameobject to the last spot under your parent object, therefore causing it to be drawn on top
https://docs.unity3d.com/ScriptReference/Transform.SetAsLastSibling.html
EDIT :unless you mean dragging it manually, so then drag it to the bottom , last item always appears on top

Unity: Drag on Screen and instantiate game object

How can I continuously instantiate Game objects by dragging on screen with unity and making the game object to follow the direction of the dragging?
For example: Drag right instantiates right, Drag up instantiates up or drag upwards at a certain angle and instantiates accordingly.
I don't know what you have done so far or how good you are in programming, but the basic principle would be
Create a DragInstantiate Script, which you put on a GameObject on your Scene, but not the one you want to Instantiate.
In the Update Method of this Script you have to check, if the left mouse button is pressed down. https://docs.unity3d.com/ScriptReference/Input.GetButtonDown.html
When you registered the click you have to save the position of the click. https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html
The next step is to check when the left mouse button is released and again save the position. https://docs.unity3d.com/ScriptReference/Input.GetButtonUp.html
From these two positions you can really easy calculate the direction, in which the object should move. https://docs.unity3d.com/Manual/DirectionDistanceFromOneObjectToAnother.html
The last thing is to instantiate the object and set it's moving direction. https://docs.unity3d.com/Manual/InstantiatingPrefabs.html

unity2d button collider collide with border when character moves

I made a button that is clickable during the game playing. As I want the button have a fixed position in the main camera, I made both the main camera and the button children of the player gameObject so that the camera will follow the character while jumping or moving, everything works fine but there also a border colliders which will prevent the character moving out of the playing area. But then the collider of the button which was made intend to make the button clickable will also collide with the border which will prevent the character moving right forward. If we set the collider of the button a trigger, it seems that the button will be triggered wherever I click the mouse on the screen, that's not what I wanted.
I know maybe I could prevent this by checking if the collided object is the button or the character, but is there a better way to do that? Thanks.
For a 2D platform game, I would add a 2D user interface in a canvas over the "map" (the layer where you have the gameobjects like the character, platforms, enemies...). So the button will be always in the same place of the screen and will never collide with any gameobject of the game.
You make take some ideas from here: https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-events-and-event-triggers
Try use new UI in Unity may it fix your issue:
https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-button

Capsule Collider Is not available in first person character settings in Unity 5

I'm learning Unity 5 and I have made simple game scene which has a coin and I wrote a code to collect a coin by first person character and when he walks towards the coin. So I dragged and dropped my code into coin layer in Hierarchy but to make this feature available I need to check the little checkbox of Is Trigger at my FPSController settings at the right side. The problem is Capsule Collider is shown at all and I really don't know why! There MUST be a section which is called Capsule Collider with Is Trigger option under the Audio Source. But as you can see ,it's not there & I don't know how to find it.. see my print screen
If it does not display in your Scene View, Enable it in the Gizmos by clicking on it.
Then under Built-in Components, make sure that Capsule Collider is selected. It show now show in the Scene View.
If it is not in the Inspector or not attached to your FPS Controller, add it by yourself.
Select the FPS Controller, Got to Component->Physics->Capsule Collider. You can then resize it.
You can add the Capsule Collider into your character yourself. All you have to do is select your character, scroll down the Inspector panel, click add component, search for Capsule Collider and select it.

Unity3d with vuforia showing 2d image when targed is detected

I have a question about the way how to show simple 2d image on top of detected marker. I have followed some tutorial to show 3d model and it works fine. there is no problem with 3d.
The problem starts when I want to add normal 2d object->sprite . When I add simple sprite I can't add texture and when I insert UI image it's added together with canvas and it is not showed when target is
detected. The original image on editor is placed then so far that it's difficult to find it.
I would be grateful if somebody can highlight me the right direction.
I need to make this image touch sensitive like a button. Clicking into it must show new scene ( I have it but under GUI.Button). The best way is to replace original marker but I can also make new sprite bigger to hide marker under it.
To help understand the answer, here's a quick rundown on how Vuforia handles marker detection. If you take a look at the DefaultTrackableEventHandler script that's attached to the ImageTarget prefab, you'll see that there are events that fire when the when the tracking system finds or loses an Image.
These are OnTrackingFound (line 67) & OnTrackingLost (line 88) in DefaultTrackableEventHandler.cs
If you want to show a Sprite when tracking, all you need to do is put the Image Target prefab (or any other) and make the Sprite a child of the prefab. The enabling and disabling should happen automatically.
However, in case you want to do something more, here's some edited code.
DefaultTrackableEventHandler.cs
//Assign this in the inspector. This is the GameObject that
//has a SpriteRenderer and Collider2D component attached to it
public GameObject spriteGameObject ;
Add the below lines to OnTrackingFound
//Enable both the Sprite Renderer, and the Collider for the sprite
//upon Tracking Found. Note that you can change the type of
//collider to be more specific (such as BoxCollider2D)
spriteGameObject.GetComponent<SpriteRenderer>().enabled = true;
spriteGameObject.GetComponent<Collider2D>().enabled = true;
//EDIT 1
//Have the sprite inherit the position and rotation of the Image
spriteGameObject.transform.position = transform.position;
spriteGameObject.transform.rotation = transform.rotation;
And the below to OnTrackingLost
//Disable both the Sprite Renderer, and the Collider for the sprite
//upon Tracking Lost.
spriteGameObject.GetComponent<SpriteRenderer>().enabled = false;
spriteGameObject.GetComponent<Collider2D>().enabled = false;
Next, your question about detecting clicks on this Sprite. Unity's Monobehaviour fires events for a lot of mouse events, such as OnMouseUp, OnMouseDown etc.
Link to Monobehaviour on Unity's API docs
What you will need is an event called OnMouseUpAsButton
Create a new script called HandleClicks.cs and add the below code to it. Attach this script as a component to the spriteGameObject that you assigned for the above.
public class HandleClicks : MonoBehaviour {
//Event fired when a collider receives a mouse down
//and mouse up event, like the interaction with a button
void OnMouseUpAsButton () {
//Do whatever you want to
Application.LoadLevel("myOtherLevel");
}
}