Instantiating Gameobject changes its shape - parent

When instantiating an object from prefab by this way (in an empty project, Unity 2020.3.2f1):
myObject = Instantiate(preObject, Parent.transform);
This one changes myObject's shape very much. Actually, I don't know why.
Found a decision:
myObject = Instantiate(preObject);
myObject.transform.parent =Parent.transform
Is this a bug or Im just that lazy, I can't read documentation?

I cant make a comment because I dont have enough rep ..
You could also set the transform as a parent and than just set the child scale to 1/parentScale.
obj = Instantiate(newObj, parent);
obj.localScale = new Vector3(1/parent.localScale.x, 1/parent.localScale.y, 1/parent.localScale.z);
If you have more than one parent with streched scale you can try the same with parent.losssyScale

Related

Getting Widget properties

How can I get widget properties given that I know the URL, language and version I want of a page. I am trying to get a property of a widget instance from a completely different project in the same solution. Is this possible?
Incase some one needs it in future. I think I have figured it out now. Remember to resolve the url before use.
TreeProvider tree = new TreeProvider();
TreeNode staticNode = tree.SelectSingleNode(siteName, url, culture);
PageInfo pi = CMSWebPartPropertiesPage.GetPageInfo(staticNode.NodeAliasPath, staticNode.DocumentPageTemplateID, culture);
PageTemplateInstance templateInstance = pi.DocumentTemplateInstance;
WebPartInstance widgetInstance = templateInstance.GetWebPart(widgetName);

Hololens Placing Objects With Spatial Understanding

I have been running SpatialUnderstandingExample scene from holo-toolkit. Couldnt figure out how to place my objects into the scene. I want to replace those small boxes that comes default with my own objects. How can I do that?
Thanks
edit: found the draw box but how do i push my object there?
edit2: finally pushed an object at the position but still code is very complicated its messing up with the size and shape of my object. Will try to make it clean and neat.
It's been a while since I've looked at that example so hopefully I remember its method name's correctly. It contains a "DrawBox" method that is called after a successful call to get a location from spatial understanding. The call that creates the box looks something like this:
DrawBox(toPlace, Color.red);
Replace this call with the following (assuming "toPlace" contains the results from the spatial understanding call and "model" contains the model you are trying to place there):
var rotation = Quaternion.LookRotation(toPlace.Normal, Vector3.up);
// Stay center in the square but move down to the ground
var position = toPlace.Postion - new Vector3(0, RequestedSize.y * .5f, 0);
// instantiate the hologram from a model
GameObject newObject = Instantiate(model, position, rotation) as GameObject;
if (newObject != null)
{
// Set the parent of the new object the GameObject it was placed on
newObject.transform.parent = gameObject.transform;
}

Can't Able to add two FrameLayout Objects in LinearLayout programmatically

while adding two FrameLayout Objects in LinearLayout Object Programmatically, getting the following exception on adding second FrameLayout object to LinearLayout object. Can anyone help.
Java.Lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
here is the code
ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WrapContent,
ViewGroup.LayoutParams.WrapContent);
MainView = new LinearLayout(_context);
MainView.LayoutParameters = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MatchParent,
ViewGroup.LayoutParams.MatchParent);
MainView.Orientation = Orientation.Vertical;
MainView.SetVerticalGravity(GravityFlags.Center);
MainView.SetHorizontalGravity(GravityFlags.Center);
_currentSwipableItemReflectionFrameLayout = _currentSwipableItemFrameLayout;
_currentSwipableItemReflectionFrameLayout.RotationX = 180;
_currentSwipableItemReflectionFrameLayout.Alpha = 0.3f;
MainView.AddView(_currentSwipableItemFrameLayout,param);
MainView.AddView(_currentSwipableItemReflectionFrameLayout,param);
AddView(MainView);
The exception says it all. One of the Views you are trying to add using AddView already has been added as a child somewhere in the View hierarchy. Hence you must remove it from its parent first before adding it elsewhere.
From the code you have posted it would seem that either or both _currentSwipableItemFrameLayout or _currentSwipableItemReflectionFrameLayout already has a parent.

Referrring to movie clips using variable strings

I want to know how to use a variable string to reference a movie clip in a hierarchy in AS2.
For example, and please forgive my newbie coding:
If my variable is defined as:
_root.MovieName = "Bob";
Then I'd like to be able to write:
_root.MovieName.ChildClip.gotoAndPlay("Label");
Where MovieName is the string "Bob" and not an actual instance called "MovieName". So Flash looks for an instance of "Bob" and goes into the child clips from there.
Is there any way to do this?
_root actually is a reference to the "root" of the movie, which also inherits a bunch of properties, it behaves like an object, so yes, you can do things like the following:
trace(_root["Bob"]); //Should return the instance.
var movieName = "Bob";
trace(_root[movieName]); //Should be the same.
I FOUND THE ANSWER!
In order to refer to movie clip instances using variables, first declare that variable as a string, then use the this[] handler. Here is the code that worked for me and the page that held it:
// CREATE THE STRING
var newString:String = "movieClipInstanceName";
// ASSUMING YOU ALREADY HAVE A MOVIECLIP WITH AN INSTANCE NAME OF "movieClipInstanceName" ON STAGE,
//CHANGE THE ALPHA OF THE MOVIECLIP TO 0
this[newString].alpha = 0;
And the page:
http://www.kirupa.com/forum/showthread.php?327501-Converting-String-to-Movie-Clip-Instance-Name
Big thanks to everyone who pitched in to help me!

Return object to original position on TouchesEnded?

I am trying to do a very basic thing but I am new to this.
Basically I have a screen with a 3 objects that can move around,
I have implemented a method which I call when TouchesMoved happens -
if object X moves over main object the object X will be hidden.
What I want to do is when the object Y is released over the main object
it will return to the position it was moved from.
should this be implemented in the TouchesEnded?
what would the method look like?
Any help would be very appreciated.
All you'd have to do here, is remember the object position in touchesBegan: and then restore the object in touchesEnded:
If you're only accepting single touches, then you can use something like this in the touchesBegan / touchesEnded methods to grab the touch...
CGPoint location = [[touches anyObject] locationInView:self];

Resources