- Home /
How to link a Main Camera via script and scripting for WindZone
I wanted to ask how can I link a main camera to a script to prevent an error occurring saying could not find a wind zone to link to Main Camera (WindzoneController) UnityEngine.Debug:LogError(Object)?
I am currently working on an environment which consists of grass and trees affected by wind however I am looking to code code the wind zone so far I have two scripts which are both attached to the wind zone object in my environment and the main camera but I get these following errors. I have posted my code underneath the errors if anyone can help me with this it will be most appreciated.
could not find a wind zone to link to Main Camera (WindzoneController) UnityEngine.Debug:LogError(Object)
NullReferenceException: Object reference not set to an instance of an object ScriptableWindzoneInterface.GetWindZoneValue (System.String MemberName) (at Assets/Standard Assets (Mobile)/ScriptsScriptableWindzoneInterface.cs:150)
IndexOutOfRangeException: Array index is out of range. (wrapper stelemref) object:stelemref (object,intptr,object) ScriptableWindzoneInterface.set_WindTurbulence (Single value) (at Assets/Standard Assets (Mobile)/Scripts/ScriptableWindzoneInterface.cs:67) WindzoneController.Update () (at Assets/Standard Assets (Mobile)/Scripts/WindzoneController.cs:27)
ScriptableWindzoneInterface
using UnityEngine;
using System.Collections;
using System.Reflection ;
// Provides a scriptable interface to a windzone
/*
*/
public class ScriptableWindzoneInterface : MonoBehaviour {
// Private vars ===================================
// The windzone component
private Component m_WindzoneComponent ;
// The system qualified type of the windzone
private System.Type m_WindzoneType ;
// Used to pass an argument to WindZone setter functions
object[] m_WindZoneArgs = new object[1] ;
// Public properties ==============================
// `radius`
public float Radius {
get
{
// Return the value of `radius`
return (float)GetWindZoneValue( "get_radius" ) ;
}
set
{
// Set the argument
m_WindZoneArgs[1] = value ;
// Set the value of `windMain`
SetWindZoneValue( "set_radius" , m_WindZoneArgs ) ;
}
}
// `windMain`
public float WindMain {
get
{
// Return the value of `windMain`
return (float)GetWindZoneValue( "get_windMain" ) ;
}
set
{
// Set the argument
m_WindZoneArgs[1] = value ;
// Set the value of `windMain`
SetWindZoneValue( "set_windMain" , m_WindZoneArgs ) ;
}
}
// `windTurbulence`
public float WindTurbulence {
get
{
// Return the value of `windTurbulence`
return (float)GetWindZoneValue( "get_windTurbulence" ) ;
}
set
{
// Set the argument
m_WindZoneArgs[1] = value ;
// Set the value of `windTurbulence`
SetWindZoneValue( "set_windTurbulence" , m_WindZoneArgs ) ;
}
}
// `windPulseMagnitude`
public float WindPulseMagnitude {
get
{
// Return the value of `windPulseMagnitude`
return (float)GetWindZoneValue( "get_windPulseMagnitude" ) ;
}
set
{
// Set the argument
m_WindZoneArgs[1] = value ;
// Set the value of `windPulseMagnitude`
SetWindZoneValue( "set_windPulseMagnitude" , m_WindZoneArgs ) ;
}
}
// `windPulseFrequency`
public float WindPulseFrequency {
get
{
// Return the value of `windPulseFrequency`
return (float)GetWindZoneValue( "get_windPulseFrequency" ) ;
}
set
{
// Set the argument
m_WindZoneArgs[1] = value ;
// Set the value of `windPulseMagnitude`
SetWindZoneValue( "set_windPulseFrequency" , m_WindZoneArgs ) ;
}
}
// Public functions ===============================
// Find and link against the windzone
public void Init () {
// Get the windzone of this game object
m_WindzoneComponent = GetComponent("WindZone");
// Ensure there was a windzone to link to
if ( m_WindzoneComponent == null ) {
// Log the error
Debug.LogError( "Could not find a wind zone to link to: " + this ) ;
// Disable this script for the remainder of the run
this.enabled = false;
// Stop here
return ;
}
// Get the system qualified type
m_WindzoneType = m_WindzoneComponent.GetType() ;
}
// Private functions =============================
// Set a WindZone property value
void SetWindZoneValue ( string MemberName , object[] args ){
// Call the setter
m_WindzoneType.InvokeMember(
MemberName ,
BindingFlags.InvokeMethod | BindingFlags.Instance ,
null ,
m_WindzoneComponent ,
args ) ;
}
// Get a WindZone property value
object GetWindZoneValue ( string MemberName ){
// Call the getter
return m_WindzoneType.InvokeMember(
MemberName ,
BindingFlags.InvokeMethod | BindingFlags.Instance ,
null ,
m_WindzoneComponent ,
null ) ;
}
}
WindzoneController
using UnityEngine;
using System.Collections;
// Allows Control Over WindZone Component
// Note: The WindZone & This Script Must Be Attached To The Same Game Object
public class WindzoneController : ScriptableWindzoneInterface {
// Use this for initialization
void Start () {
// Tell the ScriptableWindzoneInterface to initialize
base.Init() ;
// Example: Log the current settings
Debug.Log( "The Starting Settings Of The Windzone Are: Main=" + WindMain
+ ", Turbulence=" + WindTurbulence
+ ", Pulse Magnitude=" + WindPulseMagnitude
+ ", Pulse Frequency=" + WindPulseFrequency ) ;
}
// Update is called once per frame
void Update () {
// Example: Setting each of the values
WindMain = Mathf.PingPong( ( Time.time / 2.0F ) , 1.0F ) ;
WindTurbulence = WindMain ;
WindPulseMagnitude = WindMain ;
WindPulseFrequency = WindMain ;
Radius = WindMain;
}
}
Your answer
Follow this Question
Related Questions
how to change wind main property and transform of wind zone through c# script? 1 Answer
How can I change WindZone settings with a script? 4 Answers
Accessing 'Wind Main' on windzone objects thru script 0 Answers
Referencing non static variables from another script? C# 2 Answers
I need help instantiateing a bullet 0 Answers