TextMesh pro Unity: instantiated text prefab wont change it's position? - c#

I am working on a 2D game and the text prefab that I instatiate doesn't position itself over the gameObject clicked (which is the goal). I've set the Canvas as parent of the prefab via script after spawning it and it doesnt change position.
// creating hit text
GameObject canvas = GameObject.Find("Canvas");
GameObject hit = (GameObject)Instantiate(hitText, transform.position,Quaternion.identity);
hit.transform.SetParent(canvas.transform, false);
hit.transform.position = transform.position;
P.S: this sample code worked with a text made with the Unity Text Editor. Does that mean TexhMesh Pro won't support this function?

For anyone who might have this problem please understand that I made sure that the instantiated text is a child object of the canvas. The problem was that my text has been changed on start by an attached animation. The script above was working fine but the animation was changing it.
You can easely fix this problem by setting the parent of the text to an empty GameObject, which then serves as a container for the text, allowing the text to change its position relatively to it's parent only.

Related

How to add Vertical scrolling to a Canvas using script?

I want to add Vertical scrolling to a Canvas using a script.
I'm adding N number of GameObject to the Canvas and placing them one after the other.
Now I want to add a vertical scroll to the canvas but I'm not sure how to proceed.
Please Help me out.
Thank You.
GameObject instance = Instantiate (Resources.Load ("Object", typeof (GameObject))) as GameObject;
instance.transform.SetParent (GameObject.FindGameObjectWithTag ("Canvas").transform, true);
I guess what you want is a ScrollView object which scrolls vertically.
So according to Unity's documentation the best way to create the UI elements runtime is by creating a Prefab of that UI Element, set the properties according to your need.
So in your case You need to create a prefab of ScrollView which has the Horizontal set as false and Vertical set as True in the ScrollRect component and then make a prefab of that object and use it in your script to instantiate it.
public GameObject scrollPrefab;
void SpawnVerticalScrollView()
{
GameObject instance = Instantiate (scrollPrefab,GameObject.FindGameObjectWithTag ("Canvas").transform) as GameObject;
}
And as you can see I have also assigned the parent to the instantiated object right away in the same line.
I recommend you don't use the function Resources.Load, because the Unity says: "Don't use it. Use of the Resources folder makes fine-grained memory management more difficult. So you need to have a created prefab with your scrolling component and add this prefab link to your main script that controlling UI objects in the Canvas by the editor. When you need to use the scrolling prefab just use "Instantiate" function. Attache your controlling script to the Canvas object. For example:
public GameObject scrollingPrefab;
public void CreatePrefab(){
GameObject currentPrefab = Instantiate(scrollingPrefab, gameObject.transform);
}
Also, the bad way of creating objects is using the function FindGameObjectWithTag and other "Find".

Instantiated 2D text doesn't show up in Unity

Excuse me for any grammatical errors. (I know that there are many questions like this here, but I haven't found a solution yet).
I'm trying to instantiate a 2d object with a 2d text, the problem here is that the text is invisible when instantiated. Yes, I know that I have to set a canvas as parent of it but it is still not working...
Code:
Instantiate(levelAsteroid, new Vector3(-7, 2.25f, 0), Quaternion.identity,
GameObject.FindGameObjectWithTag ("Canvas").transform);
Unity hierarchy, when the object is instantiated:
Canvas settings:
Update:
I think I found the problem.
If I just instantiate the object, it works fine, the text is visible, but if I try to change the text from the script, the text becomes corrupt, after that the text won't never show up not even if I put the object in the canvas manually.
FIXED:
The problem seems to be the way I used to change the text..
Before, I used to instantiate the object and change the text from a script attached to the GameController, now I change the text from a script attached to the object that has the text as child.
Before:
(Script attached to the GameController):
public GameObject exampleOfObj;
void instantiateObj(){
object = Instantiate(exampleOfObj, new Vector3(-800, 300f, 0), Quaternion.identity);
object.transform.SetParent (GameObject.FindGameObjectWithTag ("Canvas").transform, false);
objectText.SetText ("Text: " + value);
}
I simply removed the call to the "SetText" method and I have put this into the script that is attached to the object that I'm instantiating.
I found a solution but I don't understand why it was a problem.
It's hard to figure out your issue without additional information. Possible solution is use Transform.SetParent method with worldPositionStays parameter set to false instead of instantiating on parent transform. You can find troubleshooting of similar issues in Unity Documentation. See Instantiating the UI element section.
I did exactly as you did and it is showing the text. My canvas settings are :
Render Mode : Scale Space - Camera (Camera Attached)
UI Scale Mode : Scale With Screen Size
Reference Resolution : 1920 x 1440
Match = 0.5
Also make sure when you put the object in the canvas manually, it is also showing the text.

Unity GameObject position z is mysteriously changed after object is created in script

I have once again problems with Unity. I can't spear much code due the level of secrecy agreement in the game I'm working on but I ran to this one annoying problem when running my code.
SO, I'm creating GameObjects in runtime when entering a certain view in the game. I have created a so called Reader for our game story which reads text from database, splits it to paragraphs, creates GameObject around that paraghraph text (setting text components etc. to it) and then adds that GameObject and its text to a story panel that shows all the objects as a scrollable view inside masked panel.
Hopefully you can keep up with my explanations :D
Everything works fine all the way to the point that the paragraphs are appearing correctly underneath the parent object (the view panel for the texts) and is shown correctly in the scene view, but... the problem is that, although i have made several checks in update loops, Unity just keeps giving random z position to the GameObjects setting them to
position z = -350
which makes them out of the player view and quite hard to read :D
I have debugged many times the position of the GameObjects in update giving it out z position 0 in every frame. This clearly doesn't mach the value the objects have in editor view during runtime...
Has anyone ran in to this kind of problem?
ps. I have treid to close and open Unity, load things again, nothing works.
oh and one thing to mention too is that to stretch the text to fit the size of text content and parent panel width I have used these as examples:
"Unity UI Tutorial - How to make a scrollable list" by rachetandclank3
Hmm... I somehow managed to fix this problem when I changed this:
this.paragraphObject.transform.localPosition.Set(
this.paragraphObject.transform.position.x,
this.paragraphObject.transform.position.y,0f);
to using this instead:
Vector3 newPosition = new Vector3(
this.paragraphObject.transform.position.x,
this.paragraphObject.transform.position.y, 0f);
this.paragraphObject.transform.localPosition = newPosition;
hopefully this helps others that also struggles with this kind of problem.
Source to the solutions: GameObject position.Set() not working
in Vector3 at z position try to change with gameObject.transform.position.z,
replace gameObject with the object name which position is changed mysteriously.

Getting height/width from a unity gameobject

I'm working on a version of Conway's Game of Life in Unity, and here is my setup for making the grid:
I've created a prefab of an individual cell responding to mouse click that will be the basis for creating the cell grid. I have a Empty GameObject to act as the controller to create the grid. I'm putting it in the code for the controller like so, pointing my prefab to the field:
[SerializeField]
private GameObject Cell;
private Camera _camera;
My idea was to get the dimensions of the Cell and instantiate it into a grid, with _camera pointing to the Main camera to get boundaries. However, I'm not sure how to get the height/width from GameObject. What's the best way to find this out?
I don't know if you found the answer, but the most common way is using Collider (if you have one, but mouse click needs it) or Renderer (if you have a mesh) by using:
GetComponent<Collider>().bounds.size
GetComponent<Renderer>().bounds.size
Game of life is very nice, I've written my paper for bachelor seminar about it. Have fun!

Unity: Instantiating prefab with animator

I'm making 2D platformer game using Unity 4.3.4 engine. I've created a simple prefab, which have two animations: "idle" and "death"(i used "animator") and script to control this animations.
And here's the problem: when i instantiating clones of this prefab, they always show "idle" animation and don't turn on "death" when needed.
pos = new Vector3 (-5, 4, 0) * TileSize;
newObject = Instantiate (Bonus, pos, Quaternion.identity) as GameObject;
But what is insteresting: i found a way to make animation work fine. Just add after instantiation one string like this:
newObject.animation["boxNew"].speed=1;
or this(or any string that trying to operate with "animaton"):
newObject.animation.enabled=true;
Of course i get exception at this string: "MissingComponentException: There is no 'Animation' attached to the "BonusBlock(Clone)" game object" . This is true, i really don't have Animation component, i have Animator. But why everything is working this way? Can anyone explain this?
The animator is really only used if your going full blast with the new mecanim/state flow animator, what you prolly want to do for a simple example would be to add a Animation (not animator) component to your prefab, and assign your 2 animations to the animations list in the inspector for the said animation component. Afterwards on the object you could use newObject.animation.Play("death"); to play the death animation when you want it to trigger. or use something like newObject.animation.CrossFade("death"); for a blended animation.
Found here -> http://docs.unity3d.com/Documentation/ScriptReference/Animation.html
Unity Technologies gave the answer: Before deactivating, use a default state to reset the gameObject. Discussion on Unity3d Forum
If you can read chinese, click here

Categories