I have been struggling with using methods and variables across different scripts for a while, and have done quite a bit of googling about the various ways it can be done. I have even restarted from scratch a few times.
Is there a certain location relationship you need for these things? Like is there a difference between getting variables and methods from a script on the same object versus in the same parent tree or just in the scene? (And from other scenes, if that is possible)
This code doesn’t involve it, but does declaring static or public, exc variables/methods matter when trying to get them from other codes? I have had Unity give me errors about something being static when I didn’t have anything static in the whole scene.
Error:
NullReferenceException: Object
reference not set to an instance of an
object PlayerActions.Update () (at
Assets/Content/Scripts/PlayerActions.cs:51)
My Code:
using UnityEngine;
using System.Collections;
public class PlayerActions : MonoBehaviour {
OTAnimatingSprite sprite;
// Declares User Input Keys
bool input_moveLeft;
bool input_moveRight;
bool input_jump;
// Use this for initialization
void Start () {
sprite = OT.ObjectByName("PlayerAnimations") as OTAnimatingSprite;
sprite.InitCallBacks(this);
sprite.PlayLoop("idle");
}
// Update is called once per frame
void Update () {
// -------------KEY BINDINGS-------------------
if ( Input.GetKey("space") ) // Jump
input_jump = true;
else
input_jump = false;
if ( Input.GetKey("a") || Input.GetKey("left") ) // Move Left
input_moveLeft = true;
else
input_moveLeft = false;
if ( Input.GetKey("d") || Input.GetKey("right") ) // Move Right
input_moveRight = true;
else
input_moveRight = false;
// -------------KEY BINDINGS-------------------
if ( input_moveLeft ) // -X
{
sprite.PlayLoop("move");
}
else if ( input_moveRight ) // +X
{
sprite.PlayLoopBackward("move");
}
else
{
sprite.PlayLoop("idle");
}
}
}
Code I am trying to follow:
http://www.wyrmtale.com/orthello/animations
(Scroll down to ‘C# Source Code’)
P.S.
It really confuses me for the examples that name things such as GameObject as they don’t directly say what I should be filling in with my own variables, or even what my variables should be. (I mean they do, but in a confusing way.)