find static var

Hi

Is thare a way to find all static vars in a script without knowing te name.

so I wil give the script name and it will find all static var’s name’s and return those.

Demonicmetal cs

You can, using reflection. The C# code below illustrates the idea (untested).

using UnityEngine;
using System;
using System.Reflection;

/*...*/

string scriptName = "MyScriptNameWithoutExtension";
Type scriptType = Assembly.GetCallingAssembly ().GetType (scriptName);
MemberInfo[] staticMembers = scriptType.GetMembers (BindingFlags.Static | BindingFlags.FlatternHierarchy);

Debug.Log ("Static members of " + scriptName + ":");
foreach (MemberInfo member in staticMembers)
{
    Debug.Log (member.Name);
}
using UnityEngine; 
using System; 
using System.Collections;
using System.Reflection; 

public class NewBehaviourScript : MonoBehaviour {

	// Use this for initialization
	void Start () {
	string scriptName = "mouse"; 
Type scriptType = Assembly.GetCallingAssembly ().GetType (scriptName); 
MemberInfo[] staticMembers = scriptType.GetMembers (BindingFlags.Static | BindingFlags.FlattenHierarchy); 

Debug.Log ("Static members of " + scriptName + ":"); 
foreach (MemberInfo member in staticMembers) 
{ 
    Debug.Log (member.Name); 
}
}
	}

It give’s me this error

NullReferenceException: Object reference not set to an instance of an object
NewBehaviourScript.Start () (at Assets\NewBehaviourScript.cs:12)

My guess on the cause would be that your type name is incorrect. However if you have trouble debugging code like this, I would suggest you pick up some programming books and tutorials before taking this one on.

As a warm up, you can peruse through the MSDN library

normaly i work in java

does anyone know how to do this in jscript

i got this

import System; 
import System.Reflection; 

var MemberInfo = [];
function Start () {

var scriptName = "mouse"; 
var scriptType = Assembly.GetCallingAssembly().GetType(scriptName); 
MemberInfo  = scriptType.GetMembers(BindingFlags.Static| BindingFlags.Instance); 



print(MemberInfo.length);
for(x=0;x<MemberInfo.length;x++){
print(MemberInfo[x]);

}
}

but it returns o members but 2 are static so why don’t they return

Try changing:MemberInfo = scriptType.GetMembers(BindingFlags.Static| BindingFlags.Instance);
To this:MemberInfo = scriptType.GetMembers(BindingFlags.Static);

Not sure if it’d work though.

it still return 0 but thanks

Can you print this out just to make sure we’re looking at the right class:print("CLASS NAME: " + Assembly.GetCallingAssembly().GetType(scriptName).FullName);(I think “FullName” is the property you want in JavaScript)

Does anyone know if UnityScript has the typeof() call? Might make life easier.

EDIT: Can you post your “mouse” script?

EDITx2: Can you tell us what you’re trying to do? I have a feeling this may be unnecessary or there is a simple workaround.

enum Axes {MouseXandY, MouseX, MouseY}
    var Axis : Axes = Axes.MouseXandY;

    static var sensitivityX = 15.0;
    static var sensitivityY = 15.0;

    var minimumX = -360.0;
    var maximumX = 360.0;

    var minimumY = -60.0;
    var maximumY = 60.0;

    var rotationX = 0.0;
    var rotationY = 0.0;
    var lookSpeed = 2.0;
	


    function Update ()
    {
	if (!pause.pauseactive){

        if (Axis == Axes.MouseXandY)
        {
            // Read the mouse input axis
            rotationX += Input.GetAxis("Mouse X") * sensitivityX;
            rotationY += Input.GetAxis("Mouse Y") * sensitivityY;

            // Call our Adjust to 360 degrees and clamp function
            Adjust360andClamp();
            transform.localRotation = Quaternion.AngleAxis (rotationX,Vector3.up);
			transform.localRotation *= Quaternion.AngleAxis (rotationY,Vector3.left );
          
        }
        else if (Axis == Axes.MouseX)
        {
            // Read the mouse input axis
            rotationX += Input.GetAxis("Mouse X") * sensitivityX;

            // Call our Adjust to 360 degrees and clamp function
            Adjust360andClamp();
            transform.localRotation = Quaternion.AngleAxis (rotationX,Vector3.up );
        }
        else
        {
            // Read the mouse input axis
            rotationY += Input.GetAxis("Mouse Y") * sensitivityY;

            // Call our Adjust to 360 degrees and clamp function
            Adjust360andClamp();
            transform.localRotation = Quaternion.AngleAxis (rotationY,Vector3.left );
              }
    }
}

    function Adjust360andClamp ()
    {

        if (rotationX < -360)
        {
            rotationX += 360;
        }
        else if (rotationX > 360)
        {
            rotationX -= 360;
        }   

        // Don't let our Y go beyond 360 degrees + or -
        if (rotationY < -360)
        {
            rotationY += 360;
        }
        else if (rotationY > 360)
        {
            rotationY -= 360;
        }

        // Clamp our angles to the min and max set in the Inspector
        rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
        rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    }

    function Start ()
    {
        // Make the rigid body not change rotation
        if (rigidbody)
        {
            rigidbody.freezeRotation = true;
        }
    }

this is the mouse script

wat i am trying to do is make a script that saves all the static vars from any script

The following does return all variables:

Debug.Log("All Members: " + Array(scriptType.GetMembers()));

So the problem must be with the BindingFlags. Maybe they don’t work properly in JSCRIPT?

oke thanks i guess

Why do you need to do that? How many of these scripts do you need to save?

it will be like a save to so you add the name of the script into the array and it will save the static var

So you expect the user to just type in whatever script name may or may not be in your program? How many different script states do you expect to allow the user to save this way? Is this a general purpose API that you’re distributing or is it for a specific game of yours?

i will sell the save tool so you can add it to your game and than its easy to use just add aal the scripts to the array and it will save the state of all the static var’s in the the scripts you have added when you save the game and returns theme to that state when you load them it will also save the position, rotation and velocity of that object

Ok, got you. Maybe try working on a very simple test case scenario: an empty class with only one static and one instance level field instead of a heavier mouse class.

My other suggestion is to try doing it in C#; plenty of resources and documentation for its API.

there will olso be a c# version but i want tot start with jave becaus I know java better but it can’t save C# so that@s why I still need a C#