Hello to all. I am new to Unity and scripting and I am trying hard to make things work in my app, by calling a script through another script in C#. Here is the situation. I got a C# script that does a rotation of a GameObject that it is attached to it.
Here is the code:
using UnityEngine;
using System.Collections;
public class RotateTheOrbitBheaviour : MonoBehaviour {
public GameObject Whatever;
public bool doRotation = false;
public Transform center;
public Vector3 axis = Vector3.up;
public Vector3 desiredPosition;
public float radius = 2.0f;
public float radiusSpeed = 0.5f;
public float rotationSpeed = 80.0f;
Vector3 matrixVector;
public Vector3 reset;
public Vector3 stop;
float native_width = 480;
float native_height = 320;
float rx;
float ry;
// Use this for initialization
void Start () {
float rx = Screen.width/ native_width;
float ry = Screen.height / native_height;
matrixVector = new Vector3 (rx, ry, 1);
Whatever = GameObject.Find ("MarkerObject");
}
void Update () {
if (doRotation) {
if (Whatever != null) {
transform.Rotate(new Vector3(0, 0, 50) * Time.deltaTime);
transform.RotateAround(renderer.bounds.center, new Vector3(1, -1, 0),rotationSpeed);
}
}
}
}
I also have this script that makes a GuiTexture as a touch button. I am trying to add the functionality to call the “RotateTheOrbitBheaviour” script on the touch of this GuiTexture button, but I get this error on compilation “Error CS0122: `RotateTheOrbitBheaviour.doRotation’ is inaccessible due to its protection level (CS0122)”. The error is on line 85, 36.
Here is the code on the touch button:
using UnityEngine;
using System.Collections;
public enum TouchButtonState
{
normal,
hover,
armed
}
[System.Serializable] // Required so it shows up in the inspector
public class TouchButtonTextures
{
public Texture normal=null;
public Texture hover=null;
public Texture armed=null;
public TouchButtonTextures() {}
public Texture this [ButtonState state]
{
get
{
switch(state)
{
case ButtonState.normal:
return normal;
case ButtonState.hover:
return hover;
case ButtonState.armed:
return armed;
default:
return null;
}
}
}
}
[RequireComponent(typeof(GUITexture))]
[AddComponentMenu ("GUI/Button")]
public class TouchButton : MonoBehaviour
{
public RotateTheOrbitBheaviour rb;
public GameObject messagee;
public GameObject Whatever;
public string message = "";
public string messageDoubleClick = "";
public ButtonTextures textures;
protected int state = 0;
protected GUITexture myGUITexture;
private int clickCount = 1;
private float lastClickTime = 0.0f;
static private float doubleClickSensitivity = 0.5f;
protected virtual void SetButtonTexture(ButtonState state)
{
if (textures[state] != null)
{
myGUITexture.texture = textures[state];
}
}
public virtual void Reset()
{
messagee = gameObject;
message = "";
messageDoubleClick = "";
}
public bool HitTest(Vector2 pos)
{
return myGUITexture.HitTest(new Vector3(pos.x, pos.y, 0));
}
public virtual void Start()
{
//Find Gameobject
GameObject Whatever = GameObject.FindGameObjectWithTag("markerObject");
//Grab the script
RotateTheOrbitBheaviour rb = (RotateTheOrbitBheaviour)Whatever.GetComponent(typeof(RotateTheOrbitBheaviour));
//Call the function
rb.doRotation("doRotation");
myGUITexture = GetComponent(typeof(GUITexture)) as GUITexture;
SetButtonTexture(ButtonState.normal);
}
public virtual void OnMouseEnter()
{
state++;
if (state == 1)
SetButtonTexture(ButtonState.hover);
}
public virtual void OnMouseDown()
{
state++;
if (state == 2)
SetButtonTexture(ButtonState.armed);
}
public virtual void OnMouseUp()
{
if (Time.time - lastClickTime <= doubleClickSensitivity)
{
++clickCount;
}
else
{
clickCount = 1;
}
if (state == 2)
{
state--;
if (clickCount == 1)
{
if (messagee != null message != "")
{
messagee.SendMessage(message, this);
}
//Find Gameobject
GameObject Whatever = GameObject.FindGameObjectWithTag("markerObject");
//Grab the script
RotateTheOrbitBheaviour rb = (RotateTheOrbitBheaviour)Whatever.GetComponent(typeof(RotateTheOrbitBheaviour));
//Call the function
rb.doRotation("doRotation");
}
else
{
if (messagee != null messageDoubleClick != "")
{
messagee.SendMessage(messageDoubleClick, this);
}
}
}
else
{
state --;
if (state < 0)
state = 0;
}
SetButtonTexture(ButtonState.normal);
lastClickTime = Time.time;
}
public virtual void OnMouseExit()
{
if (state > 0)
state--;
if (state == 0)
SetButtonTexture(ButtonState.normal);
}
#if (UNITY_IPHONE || UNITY_ANDROID)
void Update()
{
int count = Input.touchCount;
for (int i = 0; i < count; i++)
{
Touch touch = Input.GetTouch(i);
if (HitTest(touch.position))
{
if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
{
SetButtonTexture(ButtonState.normal);
}
else
{
SetButtonTexture(ButtonState.armed);
}
if (touch.phase == TouchPhase.Began)
{
if (touch.tapCount == 1)
{
if (messagee != null message != "")
{
messagee.SendMessage(message, this);
}
else if (touch.tapCount == 2)
{
if (messagee != null messageDoubleClick != "")
{
messagee.SendMessage(messageDoubleClick, this);
}
}
}
break;
}
}
}
#endif
}
}
Could anyone help me to make this script work? I am stuck on this for about 4 days now. Thank you all in advance for your answers.