Modify current Object and not prefab - unityscript

I'm really new to unity, This is my first project.
My problem is than when I create a new gameobject
public GameObject fancyName;
and then drag the prefab I want over the script window, if I make any changes on fancyName, instead of modifying what I see on the screen I get to modify my prefab...
What do I have to do in order to modify the Instantiate GameObject and not the prefab?

It is an expected behavior because fancyName holds a reference to your prefab so any changes you make to 'fancyName' is a direct modification to your prefab (remember you dragged & dropped your prefab to this variable right?).
You should instantiate/clone it:
var clone = (GameObject) Instantiate(fancyName);
and then
MakeChange(clone); // Do anything you want

Related

How I can open a door with animations?

I have this script:
Ray ray = new Ray (cam.transform.position, cam.transform.forward);
RaycastHit hit;
Debug.DrawRay (transform.position, ray.direction * 50f);
if (Input.GetKeyDown (KeyCode.E)) {
if (Physics.Raycast (ray, out hit, 50.0f)) {
if (hit.collider.gameObject.tag == "Door") {
Debug.Log ("YEAH");
}
}
}
How I can start animation open door?
There are a few things that you need to know before you can animate that door.
There are multiple ways to animate an object in Unity. For simple things like that you could decide to just rotate the object (but you need to understand that you will need a Coroutine or a Mathf.MoveTowards or Mathf.Lerp method to avoid instant moving when you call everything in the Update).
But you can also use an animation made by someone else in applications like Blender.
Or finally create an animation with the Unity in game editor and create an Animator to animate your door.
I think that you should use this way.
First follow this official manual:
https://docs.unity3d.com/Manual/animeditor-CreatingANewAnimationClip.html
to create the clip to open your door.
It's really intuitive and you don't even need to code.
After that you should create an Animator Controller:
https://docs.unity3d.com/Manual/class-AnimatorController.html
You will than create a new state with your animation and you will create a connection between the initial state to your "Open door" animation.
After that you just create a simple bool (in your Animator Controller).
And you will add
this.GetComponent().SetBool("nameofyourboolintheanimatorcontroller", true);
to your script (of course this is valid only if your Animator Controller is in the object assigned to your script... if not you should create a new Animator variable and assign it (for example you could make it public and assign it in the editor).

unity 2D touch event blocked by something

I have my project setup like this:
Main Camera
Physics 2D Raycaster
Event Mask: Everthing
Canvas
Graphic Raycaster
Blocking Objects: None
Blocking Mask: Nothing
2 Objects setup:
GameObject
Sprite Renderer
Rigidbody 2D
Circle Collider 2D
(my GO script)
UI
Image
Button
(my UI script)
In both my GO and UI script, I add OnPointerEnter event. and both work fine on their own. I can receive OnPointerEnter event.
But when I use a joint to drag a GO object and move to top of the UI object. My UI object OnPointerEnter blocked by the GO. I cannot receive the UI OnPointerEnter event.
I search on web and everybody ask for blocking raycast on GO to UI. But I need the reverse, I want both GO and UI receive OnPointerEnter event no matter if they overlap or not. Any hints?
P.S. something like this in 2D version, GameObject block UI Object. But I still want to receive UI Object OnPointerEnter.
Finally get what I want. Now I have 2 solutions:
using OnTriggerEnter2D
turn off Physics2DRaycaster LayerMask GO when dragging GO
1. using OnTriggerEnter2D
When drag GO. Shoot event to UI and tell which GO is dragging.
Add rigibody2D(isKinematic and Freeze xyz) and 2Dcollider(is trigger) as component in UI object.
OnTriggerEnter2D receive my GO collider and check if it is the dragging GO.(doing this because I excatlly want the UI get my only dragging GO).
2. turn off Physics2DRaycaster LayerMask GO when dragging GO
use this code:
using UnityEngine.EventSystems;
public void turnOffLayerMask(string layerMaskName)
{
Physics2DRaycaster p2drc = Camera.main.GetComponent();
LayerMask layerMask = p2drc.eventMask;
LayerMask disableLayerMask = 1 << LayerMask.NameToLayer(layerMaskName);
p2drc.eventMask = layerMask & ~disableLayerMask;
}
public void turnOnLayerMask(string layerMaskName)
{
Physics2DRaycaster p2drc = Camera.main.GetComponent();
LayerMask layerMask = p2drc.eventMask;
LayerMask enableLayerMask = 1 << LayerMask.NameToLayer(layerMaskName);
p2drc.eventMask = layerMask | enableLayerMask;
}
turn off GO layerMask when dragging GO. Turn On back when drag end. The raycast can go through GO to UI and receive OnPointerXXX events.
I think the EventSystem auto. choose EITHER Physical raycast or Graphic raycast to detect GO/UI objects. So you can only receive either one set of event(Non-UI/ UI). Is it correct?
It seems that I search on web and many people using OnMouseXXX (or other method) instead of Event system(i.e. OnPointerXXX). So they can "touch through" GO to UI.

Unity UI Button not calling the Onclick method

I have a button under a canvas that is suppose to restart the scene. But instead its not calling the intended function of attached script.
On Unity Forums, I found solutions like it should be at higher hierarchy in canvas, and the canvas should have a graphic raycast and so-on.
But it still isn't working, although its on click array detects the intended method it is suppose to call.
Scene Editor with Canvas Selected:
Scene Editor with Button Selected:
Remove the canvas component from your button.
Make sure that there is a GraphicRaycaster on your canvas, and that there is an EventSystem object somewhere in the hierarchy.
(Both should have been added when you first added the canvas, but things get lost)
The screenshot that shows your button being selected, also shows, that you did pick a gameobject for the OnClick event, but you didn't pick a function from the drop down right next to that field, it says no function.
Instead of selecting the C# class, select the GameObject that
btnSceneSelect is attached to.
If btnSceneSelect is not attached to a GameObject, attach it to one
(other than the button).
Taken from this site

Not able to translate gameObject when Animation is already added to it using Animator in Unity2D Sprite

I am able to translate the gameObject from one point to other by the OnMouseDrag() Event have no problem in doing that. But when i have added Animation to the GameObject Using the Animator Im not able to handle its Dynamic translate property when the Drag event take place.
Details:-
The Animation that i have create for the Game object is that it gets Translate from the Outer side of the screen to a side of the screen, This happens on the launch of the scene. (This happens properly)
Now when i want to dynamically translate the game object on the mouse drag it doesn't translate. (if i have not added the animator to the object it translate with mouse drag with out an issue)
Can any Tell me why it is so ?
Thanks in advance.
Did you try making Gameobject with animation,a child of an empty Gameobject and then translating that parent gameobject.

Unity apply idle animation to model

I created a model in Blender and posed it the way I want it to look. The character will never move and no animations will ever change.
I exported the character with animations to an FBX and imported it into Unity 3D. After dropping the character into the scene, the character's pose is back to the default take.
How can I force the character to use the pose that I want? Do I have to do it in script or is there a setting that I can change to make it play the idle animation at all times?
Putting your model in the scene will not invoke idle animation, animation and rendering is two separate thing. To always play a certain animation on a model you have three option -
1. Via Legacy Animation -
Select you model in the scene, remove the default Animator component and add a new Animation component. (Animator and Animation are two different component type.) It has an option of selecting an animation clip. Drop your animation clip there and choose 'Play Automatically'.
2. Via Script -
You can add Animation component, attach multiple animation and select which one to play via script too.
You can also do this for meca-anim way which is explained in the next point.
3. Via Meca-Anime
Every model has a default Animator component. It needs an animation controller object to run. Create an animation controller (Click on create button in project view). Attach the controller to the Animator component. Double click on the controller to open Animator view. Drop your clip there and make it as default. The default animation plays automatically now. Using Meca-anime has many advantage over legacy way, which is beyond the scope of this answer. Check the following link instead -
Mecanim vs Legacy Animation

Resources