- Home /
Setting a class-wide attribute at startup in Boo
In my main Unity script I access other game objects frequenty, e.g. the camera. Since I do not want to call GameObject.Find("Main Camera") in every Update(), I want to keep a reference to the instance as a class-local variable.
However when I try
import UnityEngine
class PlayerBehaviour (MonoBehaviour):
main_camera as GameObject = GameObject.Find("Main Camera")
I get "UnityException: You are not allowed to call this function when declaring a variable. Move it to the line after without a variable declaration."
When I move this to Start()
import UnityEngine
class PlayerBehaviour (MonoBehaviour):
def Start ():
main_camera = GameObject.Find("Main Camera")
return
I get "Assets/PlayerBehaviour.boo(25,9): BCE0005: Unknown identifier: 'main_camera'."
How can I assign a local variable to the instance of another GameObject at initalisation, using Boo?
EDIT: The camera is just an example. I am looking for a Boo technique to cache a reference to any GameObject.
Answer by DannyB · May 17, 2013 at 03:31 PM
I am writing in C#, not Boo, but my guess is that the solution would be the same.
Try separating the variable declaration and assignment.
Instead of
main_camera as GameObject = GameObject.Find("Main Camera")
Try
main_camera as GameObject
def Start ():
// This line should go in Awake() or later
main_camera = GameObject.Find("Main Camera")
return
This line should also work, if it is in Awake() or later, but then your variable is local to Awake()/Start() and not global, but you can test with this, just to see if its working.
main_camera as GameObject = GameObject.Find("Main Camera")
(I am assuming varname as type is how you declare variables in Boo)
Separating declaration and binding does the trick. That is a bit counterintuitive in Boo. It also works in Start(). Thanks. If you like, you can upvote the question.
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Rotate around object at a given angle 2 Answers
Sector scanning in Unity 3 Answers
First person camera restriction. 1 Answer
How calculated ViewportToWorldPoint 0 Answers