Unity3D : Unable to change TextMesh in instantiated object - unity3d

I instantiate an object and change the text mesh inside it using the below code.
GameObject folder = (GameObject)Instantiate(prefab,<newLocation>,Quaternion.identity);
folder.name = "Folder1";
TextMesh content = GameObject.Find("Folder1").transform.GetChild(0).transform.GetChild(3).
gameObject.GetComponent<TextMesh>();
content.text = "New Content";
But, when I run the scene, the text mesh still has the value from the prefab and is not updated. Has anyone faced a similar issue or any idea on how to resolve it?

You mentioned that the item gets updated properly but then when you create new ones, it does not work anymore.
I would think your problem is that you are naming them with the same name "Folder1". Then you are looking for an object called Folder1 and the first one is returned.
GameObject folder = (GameObject)Instantiate(prefab,<newLocation>,Quaternion.identity);
folder.name = "Folder1";
TextMesh content = folder.transform.GetChild(0).transform.GetChild(3).
gameObject.GetComponent<TextMesh>();
content.text = "New Content";
this could fix your problem. Notice this is no longer looking for an item but using the reference from the new object.

Are you sure you're getting the right object? Your GetChild's are a bit worrying. Maybe you can try giving your object a tag and searching for that instead.
So, you set the tag of the gameobject that has your textmesh to HasTextMesh and then in your code do:
GameObject folder = (GameObject)Instantiate(prefab,<newLocation>,Quaternion.identity);
TextMesh content = GameObject.FindWithTag("HasTextMesh").GetComponent<TextMesh>();
content.text = "New Content";

Related

unitŠ½ creates an extra clone on stage

added a tilemap to the scene, and for some reason there are two of them, there is only one in the inspector, but if you move the coordinates, then there will be another map on the background and for some reason the character was also cloned, what could be the problem?
I thought maybe the link to the prefab was wrong or it was added to the stage in the wrong way, but everything seems to be right
int MapNumber = GameState.currentRoom;
Tilemap map = GridsMap[MapNumber].GetComponent<Tilemap>();
CurrentMap = Instantiate(map);
CurrentMap.transform.position = Anchor;

Where is the camera component located for ACharacter?

So I just started playing around with Unreal Engine 4. I would like to learn as much as I can, so I started with a blank C++ project.
I created a new Character class for my player character, then created a Blueprint based on this class.
The character Blueprint (or some of it's components seem to have a UCameraComponent attached to it, since after making the keybindigs for movement and look up/turn I could already use my mouse to navigate the camera.
My question is, where is this UCameraComponent located? When I open the Blueprint, it seems like it doesn't have a CameraComponent in there. I also tried searching for it in the source code of ACharacter, but couldn't find anything.
I would like to adjust the camera position related to the character because right now this camera is right inside my character mesh.
You have to add it to your class manually.
In YourCharacter.h:
UPROPERTY(EditAnywhere, Category = "Components")
USpringArmComponent* SpringArm = nullptr;
UPROPERTY(EditAnywhere, Category = "Components")
UCameraComponent* Camera = nullptr;
In YourCharacter.cpp constructor:
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("Spring Arm"));
SpringArm->SetupAttachment(RootComponent);
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
Camera->SetupAttachment(SpringArm);

Why the mesh has been marked as non-accessible when adding MeshCollider from assetBundle and how to solve it?

I have a scriptable asset containing a fxbModel on a GameObject variable and with it i'am doing this:
model = Instantiate(caAsset.model, origin);
fbxComponents.All(fbxComponent => fbxComponent.gameObject.AddComponent<MeshCollider>());
Is the asset loaded from Ressources, everything works well.*
But is the asset from a AssetBundle** the following error occurs.
CollisionMeshData couldn't be created because the mesh has been marked as non-accessible.
Mesh asset path "" Mesh name "Flanschdeckel_low"
UnityEngine.GameObject:AddComponent()
(*)
MyScriptableAsset caAsset = Resources.Load(scriptableObjectName);
(**)
MyScriptableAsset caAsset = bundle.LoadAsset<MyScriptableAsset>(scriptableObjectName);
There's a read/write enabled property on fbx files on the model tab under mesh sub text if you tick that box it'll probably work. Check right corner on image for better understanding.enter image description here

How to associate Price with Different item in unity

I am newbie in unity. i am facing a small Problem. I have a different number of items and i want to associate Different Price with them .
but i dont know How to access Text(script) component in Unity? ?????
Lets says Here is the book shop every book Container Have different price
Here what i try to do but i don't know how to access this text Using Script
Thanks in advance
Find the "NumericalScore" GameObject with GameObject.Find then get the Text component from it with the GetComponent function.
//Find the Text GameObject
GameObject textObj = GameObject.Find("NumericalScore");
//Get the Text component
Text text = textObj.GetComponent<Text>();
string bookText = text.text;
Now, let's say that the each book has different names but with a Child text with the-same name called "NumericalScore", find the book name first then use transform.Find to find the child Text GameObject before using the GetComponent function to get the Text component.
//Find Book
GameObject book = GameObject.Find("BookName");
//Find Child Text GameObject
GameObject textObj = book.transform.Find("NumericalScore").gameObject;
//Get the Text component
Text text = textObj.GetComponent<Text>();
string bookText = text.text;

Unity3D - Create a NGUI Button

I'm working on some project where I need to create on the fly, buttons with NGUI.
I found some answers but none of them could help me. Iknow it's simple but according to what I found
on http://www.tasharen.com/forum/index.php?topic=81.0
and in NGUI script (UICreateWidgetWizard.cs), like:
UILabel lbl = NGUITools.AddWidget<UILabel>(go);
it's still not working..
An my code is the following:
UIButton button = NGUITools.AddWidget<UIButton>(parent);
Thanks for yout help guys !
Either as #pfranza suggests, create a prefab of your UIButtons that you can reference as a public object in your script, then to create it use the
GameObject newButton = NGUITools.AddChild(mParentGameObject, mButtonPrefab);
Alternatively, you can fully create it at runtime if you wish:
UISprite mButtonSprite = NGUITools.AddSprite(mParentGameObject, mAtlas, "Button");
//Button is the name of the sprite in the atlas
mButtonSprite.MakePixelPerfect();
NGUITools.AddWidgetCollider(mButtonSprite.gameObject);
mButtonSprite.gameObject.AddComponent<UIButton>();
//add any further components/set up you like.
You have to create a prefab out of a button that you want to use as a template; Attach that prefab to your script as a GameObject. Then in your script you can clone that object and activate it.