I have an issue with the GetComponent(GUIText) the error i get is
There is no 'GUIText' attached to the "#######COUNTER(Clone)" game object, but a script is trying to access it.
Here is my code:
var UItecxt = GameObject.Find("#######COUNTER(Clone)");
var txtconvert = UItecxt.GetComponent(GUIText);
print(txtconvert);
txtconvert.text = counternumb.ToString();
I HAVE a GUIText on my clone! What is the issue? Thanks!
Your problem is that there is no GameObject named "#######COUNTER(Clone)" cloned in the scene. Run my code below and you will notice.
var UItecxt = GameObject.Find("#######COUNTER(Clone)");
var txtconvert : GUIText;
if(UItecxt != null)
txtconvert = UItecxt.GetComponent(GUIText);
else
Debug.Log("There was no GameObject with the name '#######COUNTER(Clone)' in the scene");
To fix it just make sure you do have a GameObject with that name.
Your question is not detail enough to determine the problem.
Still, i assumed the gameobject that you mentioned should be a prefab that you instantiated.
Where you put or attach the sciprt? Make sure it is attached to the prefab that you instantiated.
Also, you can try use direct reference the component instead of using gameobject.Find.
It is easier for you to drag and drop element at the inspector.
Related
I'm learning Unity by trying to do things and then when ofc failing (Because that's life about XD) then I'll try to find some scripts to learn from.
I was trying to make a smooth platformer controller and then ran into a video from Brackeys
I'm having trouble understanding this line of code:
if (colliders[i].gameObject != gameObject)
{
m_Grounded = true;
if (!wasGrounded)
OnLandEvent.Invoke();
}
(Full script can be found here: https://github.com/Brackeys/2D-Character-Controller/blob/master/CharacterController2D.cs)
It's that line 54 the one that is making me have problems.
if (colliders[i].gameObject != gameObject)
What is he comparing? The first one is the gameobject attached to the collider but the second one is the default class, can someone explain what is he trying to do here?
Notice that Monobehvaiour inherits from component. So it has access to all its properties gameObject, transform ..etc. Check component documentation
So in the snippet
if (colliders[i].gameObject != gameObject)
{
m_Grounded = true;
if (!wasGrounded)
OnLandEvent.Invoke();
}
you are comparing the specific array's object (colliders[i].gameObject) with the current monobehaviour the code is running in (gameObject or this.gameObject).
In a class, you can access all the public and protected members it inherits from skypping the this keyword, as it can be understood you refer to the current instance by default.
Hope that makes sense.
Hi i have a Robot Prefab, with a NPC script that is linked to dialogue sentences.
I am retrieving the information about dialogues (using a loader script) from a server API, and then instantiating both the robot gameobject and the sentences. For example for instantiating the gameobject:
GameObject newObject = GameObject.Instantiate(Resources.Load("prefabs/" + item.gameObject.name) as GameObject, parentProps);
newObject.transform.localPosition = new Vector3(loaded.x, loaded.y, loaded.z);
newObject.transform.localEulerAngles = new Vector3(0, 90f, 0);
However, i do not know how to access the dialogue and update it. Could someone tell me how to update the instantiated value of the sentences here (marked it with red arrows on the image below)?
May be a basic question, but am new. Thanks!
You can create a public function in Npc class and call it after you instantiated it.
GameObject newObject = GameObject.Instantiate(Resources.Load("prefabs/" + item.gameObject.name) as GameObject, parentProps);
newObject.transform.localPosition = new Vector3(loaded.x, loaded.y, loaded.z);
newObject.transform.localEulerAngles = new Vector3(0, 90f, 0);
newObject.GetComponent<Npc>().UpdateDialog();
On your Robot GameObject use GetComponent to get the NPC script object. Then you are able to modify the values. Make sure that the visibility of your variables is either public or has getters/setters.
I have created a code file in Unity and assigned it to an empty GameObject I have placed in the scene:
var obj = new GameObject("Sample");
obj.transform.position = new Vector3(0, 0, 0);
var text = obj.AddComponent<TextMesh>();
text.text = "Hello world";
When I run the scene, I can see the text. And that is my problem: I did not specify anywhere in code to add obj to the scene, but it gets placed automatically apparently.
This can be a problem if I want to introduce an object later than instantiation time.
What am I doing wrong? How can this be achieved? What are the patterns/best-practices here?
Immediate fix:
Use obj.SetActive(false) to temporarily disable the object and then use obj.SetActive(true) when you need the object to be active.
Other solutions / best practices:
Create the object you desire in the scene, save it as a prefab (prefabricated object) and then only instantiate it when you need it. Here's a link for further reading into the prefab system. https://docs.unity3d.com/Manual/Prefabs.html
Object pooling is typically used when you will have a bunch of the same objects (like lasers, bullets, etc). Watching this video may be of help: https://www.youtube.com/watch?v=tdSmKaJvCoA
I have a Canvas (World Space Render mode) with a Text and a Button component displayed in a tridimensional space (it's a VR app). The canvas instantiated at runtime using a prefab.
I get a reference to the Text object using:
_codeTextLabel = canvasPrefab.transform.Find("CodeTextLabel").gameObject.GetComponent<Text>();
I want to update the text at run-time using:
void Update()
{
_codeTextLabel.text = _codeText;
}
where _codeText is just a variable I update based on specific events.
The problem is that the Text gets updated only the first time, but if I try to change the variable nothing happens. I have tried several combinations and also the method _codeTextLabel.SetAllDirty() but it doesn't work.
The only way to update the text is to re-instantiate the prefab.
Are you instantiating your prefab before setting the values. If you are storing the _codeTextLabel reference before instantiating then your reference will point to the prefab not the runtime object. I can't see the rest of your code, so I can't say for sure. (I would have asked as a comment, but as I'm new I don't have the reputation to do so)
edit: I did a test to try and recreate your problem. I made the following script and it appears to work as expected. CanvasPrefab is a worldspace canvas with a UnityEngine.UI.Text component attached. (The script is attached on an empty game object in the scene btw)
public class ChangeText : MonoBehaviour
{
public GameObject CanvasPrefab;
private GameObject runtimeCanvas;
public string runtimeText = "something";
private Text textRef;
// Start is called before the first frame update
void Start()
{
runtimeCanvas = GameObject.Instantiate(CanvasPrefab);
textRef = runtimeCanvas.GetComponentInChildren<Text>();
}
// Update is called once per frame
void Update()
{
textRef.text = runtimeText;
}
}
as long as you did something wrong, It works absolutely so I guess there are several cases
Failed to do "_codeTextLabel = canvasPrefab.transform.Find("CodeTextLabel").gameObject.GetComponent();"
'_codeTextLabel' lost reference from 'GameObject.
Doesn't change runtimeText' change at all
Subscription of events failed I mean, your updating scripts doesn't get proper event to update that text.
Without codes, this is only thing I can guess for yours so please check above I hope there is case among above.
I am using Vuforia's SDK for Unity 3D platform. I am trying to set the TrackableName dynamically at runtime. I found a code
GameObject prefab = Instantiate(imageTarget) as GameObject;
ImageTargetBehaviour imgTargetBeh = prefab.GetComponent();
imgTargetBeh.DataSetPath = "QCAR/Test.xml";
imgTargetBeh.TrackableName = "Daddy";
imgTargetBeh.mInitializedInEditor = true;
myModel.transform.parent = prefab.transform;
The problem is I am getting error with DataSetPath, TrackableName, mInitializedInEditor.
The ImageTargetBehaviour class does not have above mentioned properties.
So how/where to set the properties?
Thanks,
Sris
I can't yet global comment, so I'll ask here for more info and update this when I have it.
The second line worries me, because you're not telling Unity what kind of component to get. Try GetComponent(typeof(ImageTargetBehaviour)) instead of the null parameters.
The problem is I am getting error with DataSetPath, TrackableName, mInitializedInEditor. The ImageTargetBehaviour class does not have above mentioned properties. So how/where to set the properties?
If the ImageTargetBehaviour class doesn't have these properties, then why are you trying to access them? Does the documentation instruct you to do so? If so, then something is wrong with your ImageTargetBehaviour.js script.