Hi guys
trying to get component from children. Tried 3 ways but best result has been a null reference…
using UnityEngine;
using System.Collections;
public class wizglow : MonoBehaviour {
Animator dar;
NavMeshAgent duh;
float who;
GameObject de;
MeshRenderer mr;
void Start(){
de = GameObject.Find ("wizzyold");
dar = de.GetComponent<Animator> ();
1 mr = de.GetComponent ("runes/runes01/Mesh").GetComponent < MeshRenderer> ();//null reference
2 mr = de.Find ("runes/runes01/Mesh").GetComponent<MeshRenderer> ();
3 mr = de.GetComponentsInChildren< MeshRenderer> ();
}
}
The situation is i have a parent GO " wizzyold" with a GO “runes” with 26 runes01-26 each with a mesh renderer its that mesh rendere i want to get hold of.
.
Is the parent the only GameObject called “wizzyold” in the scene?
Also, you shouldn’t be nesting GetComponent calls like you are on line 15. It looks like you’re getting GameObjects and Components mixed up. GameObjects contain Components. It looks like your first call to GetComponent(…) on line 15 is actually trying to get a child GameObject with a given path.
Yeah - not quite on the right track there. Something more like:
private void Start()
{
de = GameObject.Find("wizzyold");
MeshRenderer[] meshRenderers = de.GetComponentsInChildren<MeshRenderer>();
// I.E. AND THEN DO SOME STUFF WITH THEM
foreach (MeshRenderer thisMeshRenderer in meshRenderers)
{
}
}
I am trying to call a game object inside a gameobject inside a gameobject . I placed a empty game object(runes) in the original gameobject (wizzyold) then placed more gameobjects (runes1-26) in that gameobject and the component (mesh renderer is on that last gameobject (runes1-26) under Mesh…
Ooooh you mean maybe i should be doing multiple gameobject .Finds?
Or you can iterate over children via the Transform. There’s a section in the manual about accessing GameObjects which goes over the different methods available if you haven’t checked it out already.
Or, you could make a serializable array in your script and put the runes in it in the Inspector.
In any case, yeah, you get GameObjects and Components differently, where you’re trying to get them the same way.
Like @angrypenguin said, it might also really depend on if there will just be one of these objects or more. For example, if there’s just one - why reference the top level at all? You could jump right into the Runes something like:
Transform runesTransform = GameObject.Find("runes").transform;
foreach (Transform thisChild in runesTransform)
{
MeshRenderer thisMeshRenderer = thisChild.GetComponent<MeshRenderer>();
if (thisMeshRenderer)
{
...etc
}
}
Not really so clued in on coding so those suggestion all make my head spin. Yea after I read your post It clicked that they were gameobjects i amended my script but still getting something wrong it does reference the right name now in the debug but not sure how to call the renderer.
using UnityEngine;
using System.Collections;
public class wizglow : MonoBehaviour {
Animator dar;
NavMeshAgent duh;
float who;
GameObject de;
MeshRenderer mr;
void Start(){
de = GameObject.Find("wizzyold/runes/runes01");
dar = de.GetComponent<Animator> ();
////mr = de.GetComponent ("wizzyold/runes/runes01/Mesh").GetComponent < MeshRenderer> ();//null reference
mr = de.GetComponent < MeshRenderer> ();
mr.Renderer = enabled;
de = GameObject.Find("wizzyold/runes/runes01");
MeshRenderer[] meshRenderers = de.GetComponentsInChildren<MeshRenderer>();
Debug.Log (de.name);
// I.E. AND THEN DO SOME STUFF WITH THEM
foreach (MeshRenderer thisMeshRenderer in meshRenderers)
{
enabled = true;//did not do anything.
}
}
My latest clumsy attempts following the advice lol.
Tried it just adding enabled=true but that didnt work so tried to path towards the correct gameobject but also no luck maybe the enabled=true line is not specific enough?
Also keep in mind that they’re going to be enabled by default, right? So setting them enabled = true SHOULD have no effect, unless you’ve disabled them?
Also keep in mind that you have to specify what you’re enabling. If you don’t specify an object all you’re doing is changing the enabled field on the current object… probably not what you want.
Also, why are you specifically accessing the MeshRenderer component? Do you just want to have them appear and disappear? If so, change that to just access the Renderer component.
Out of curiosity, just try the below, switching enabled/disabled depending on what you want. I think you might be overcomplicating things and maybe this is all you’re trying to do?
using UnityEngine;
using System.Collections;
public class wizglow : MonoBehaviour
{
private Transform _de;
private void Start()
{
_de = GameObject.Find("runes").transform;
foreach (Transform thisChild in _de)
{
Renderer thisRenderer = thisChild.GetComponent<Renderer>();
if (thisRenderer)
{
thisRenderer.enabled = false;
}
}
}
}
using UnityEngine;
using System.Collections;
public class wizglow : MonoBehaviour {
private Transform _de;
Animator dar;
NavMeshAgent duh;
float who;
//GameObject de;
MeshRenderer mr;
private void Start()
{
_de = GameObject.Find("runes1").transform;
foreach (Transform thisChild in _de)
{
Renderer thisRenderer = thisChild.GetComponent<Renderer>();
if (thisRenderer)
{
thisRenderer.enabled = false;
}
}
}
}
Hey Steve that did the trick I just had to change runes to runes1 to get the right directory. Unfortunately i don,t understand much of the code have to get my reading done on this. It does render them all and disable them all at the same time but need to do them one by one depending on a random number given. So if number =5 enable Mesh5 etc and no need to loop the rest but not sure how to do that within this code.
Hmm… you shouldn’t have to change it to rune01 - “runes” should have worked as you want to iterate through all 26 of the child GameObjects under “runes” right?
Runes is the empty game object under it there is another game object called runes 1 and it hold 26 meshes i could probably put them all under runes i guess and drop the middleman .
Ok now all under runes but don’t want to iterate through them all just need 1 active at any given time .