- [email protected]
- Posts: 19
- Joined:
Gameobjects and components from codebehind
Is it impossible to get a reference to gameobjects and components from codebehind?
As in, NOT using Viewmodel
Thx in advance :)
As in, NOT using Viewmodel
Thx in advance :)
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: Gameobjects and components from codebehind
Can't you just use GameObject.Find() and then access the Component you need?
- [email protected]
- Posts: 19
- Joined:
Re: Gameobjects and components from codebehind
I am so stupid, I had no idea you could do that. I keep forgetting that I am not in a real WPF-land, I am in Unity WPF-land so ofc I have access to Unity objects.
Damn this engine is epic. I love it!
Is this the fastest way to do it btw?
GameObject mainCameraGameObject = GameObject.Find("Main Camera");
mainCameraGameObject.GetComponent<MyComponent>();
It seems I have to first search the object, which in this case is the same object as NoesisEngine is a component of, so I thought maybe I didn't have to search through all the objects in Unity, which this does.
Can't I do something like
thisGameObject.GetComponent<MyComponent>(); ?
Damn this engine is epic. I love it!
Is this the fastest way to do it btw?
GameObject mainCameraGameObject = GameObject.Find("Main Camera");
mainCameraGameObject.GetComponent<MyComponent>();
It seems I have to first search the object, which in this case is the same object as NoesisEngine is a component of, so I thought maybe I didn't have to search through all the objects in Unity, which this does.
Can't I do something like
thisGameObject.GetComponent<MyComponent>(); ?
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: Gameobjects and components from codebehind
If you have a MonoBehavior script attached to the "Main Camera" object where the NoesisView is also connected you can access it by calling GetComponent:
From there you can access any element in the UI tree if that is what you are trying to do.
Code: Select all
public class MyComponent : MonoBehavior
{
void Start()
{
NoesisView view = GetComponent<NoesisView>();
// view.Content is the root UI element in the XAML
// ...
}
}
- [email protected]
- Posts: 19
- Joined:
Re: Gameobjects and components from codebehind
Ok thx, but I meant from the code behind xaml file. Fx. UserControl, not a Monobehavior :)
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: Gameobjects and components from codebehind
In that case you should use the GameObject.Find approach, that's the best I can think of.
Of course, if this is something you are going to do many times you should store the result of that search in your user control to do it only once.
Of course, if this is something you are going to do many times you should store the result of that search in your user control to do it only once.
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 29 guests