So i have a cabinet with 3 shelves.
What i´m doing: I have a script i call “Test” which is located on my players Camera Object which sends out a Raycast. When the Raycast hits the top shelf in the Cabinet it should tell it to call the Function “TheTopCabinetOpens()” which is a function inside my Cabinet objects Script. I have an animation called TopCabinetOpens and one called TopCabinetCloses on the Shelf itself. I also have the middle and bottom Shelf with the same setup as the top one.
But what happens is the following:
The animation state TopCabinetOpens could not be played because it couldn’t be found!
Please attach an animation clip with the name ‘TopCabinetOpens’ or call this function only for existing animations.
UnityEngine.Animation:CrossFade(String)
And
The animation state TopCabinetCloses could not be played because it couldn’t be found!
Please attach an animation clip with the name ‘TopCabinetCloses’ or call this function only for existing animations.
UnityEngine.Animation:CrossFade(String)
What i want it to do: When the Ray i send out of my player hits the Top shelf with the tag “topCabinet” it should tell the “Cabinets” Script to call the function “TopCabinetOpens” and then mark it with the var: topCabinetIsClosed = false.
Cabinets Script
```javascript
**#pragma strict
var theLowCabinet : Transform;
var theMidCabinet : Transform;
var theTopCabinet : Transform;
//var topCabinetIsClosed = true;
//var midCabinetIsClosed = true;
//var lowCabinetIsClosed = true;
private var drawGUI = false;
function OnGUI ()
{
if (drawGUI == true)
{
GUI.Box (Rect (Screen.width0.5-50, Screen.height0.5-25, 170, 22), “Left click to open the cabinet”);
}
}
function TheTopCabinetOpens()
{
theTopCabinet.animation.CrossFade(“TopCabinetOpens”);
//yield WaitForSeconds(0.1);
// topCabinetIsClosed = false;
}
function TheTopCabinetCloses()
{
theTopCabinet.animation.CrossFade(“TopCabinetCloses”);
//yield WaitForSeconds(0.1);
//cabinetIsClosed = true;
}
function TheMidCabinetOpens()
{
theMidCabinet.animation.CrossFade(“MidCabinetOpens”);
//yield WaitForSeconds(0.1);
//topCabinetIsClosed = false;
}
function TheMidCabinetCloses()
{
theMidCabinet.animation.CrossFade(“MidCabinetCloses”);
//yield WaitForSeconds(0.1);
//cabinetIsClosed = true;
}
function TheLowCabinetOpens()
{
theLowCabinet.animation.CrossFade(“LowCabinetOpens”);
//yield WaitForSeconds(0.1);
//lowCabinetIsClosed = false;;
}
function TheLowCabinetCloses()
{
theLowCabinet.animation.CrossFade(“LowCabinetCloses”);
//yield WaitForSeconds(0.1);
//cabinetIsClosed = true;
}**
** **Test Script** **
javascript
**#pragma strict
var hit : RaycastHit;
var Reach : float = 5.0;
var RayHit : boolean;
var cabinets : Cabinets;
var topCabinetIsClosed = true;
var midCabinetIsClosed = true;
var lowCabinetIsClosed = true;
var door : BasicDoor;
var doorIsClosed = true;
private var drawGUIOpen = false;
private var drawGUIClose = false;
function Update ()
{
var fwd = transform.TransformDirection (Vector3.forward);
Debug.DrawRay(transform.position, fwd * Reach, Color.red);
if (Physics.Raycast (transform.position, fwd, hit, Reach) && hit.transform.tag == “topCabinet” && topCabinetIsClosed == true)
{
RayHit = true;
cabinets = hit.collider.gameObject.GetComponent(“Cabinets”);
drawGUIOpen = true;
if (Input.GetKeyDown(“mouse 0”))
{
cabinets.TheTopCabinetOpens();
topCabinetIsClosed = false;
drawGUIOpen = false;
}
}
else if (Physics.Raycast (transform.position, fwd, hit, Reach) && hit.transform.tag == “topCabinet” && topCabinetIsClosed == false)
{
RayHit = true;
cabinets = hit.collider.gameObject.GetComponent(“Cabinets”);
drawGUIClose = true;
if (Input.GetKeyDown(“mouse 0”))
{
cabinets.TheTopCabinetCloses();
topCabinetIsClosed = true;
drawGUIClose = false;
}
}
if (Physics.Raycast (transform.position, fwd, hit, Reach) && hit.transform.tag == "midCabinet" && midCabinetIsClosed == true)
{
RayHit = true;
cabinets = hit.collider.gameObject.GetComponent("Cabinets");
drawGUIOpen = true;
if (Input.GetKeyDown("mouse 0"))
{
cabinets.TheMidCabinetOpens();
midCabinetIsClosed = false;
drawGUIOpen = false;
}
}
else if (Physics.Raycast (transform.position, fwd, hit, Reach) && hit.transform.tag == "midCabinet" && midCabinetIsClosed == false)
{
RayHit = true;
cabinets = hit.collider.gameObject.GetComponent("Cabinets");
drawGUIClose = true;
if (Input.GetKeyDown("mouse 0"))
{
cabinets.TheMidCabinetCloses();
midCabinetIsClosed = true;
drawGUIClose = false;
}
}
if (Physics.Raycast (transform.position, fwd, hit, Reach) && hit.transform.tag == "lowCabinet" && lowCabinetIsClosed == true)
{
RayHit = true;
cabinets = hit.collider.gameObject.GetComponent("Cabinets");
drawGUIOpen = true;
if (Input.GetKeyDown("mouse 0"))
{
cabinets.TheLowCabinetOpens();
lowCabinetIsClosed = false;
drawGUIOpen = false;
}
}
else if (Physics.Raycast (transform.position, fwd, hit, Reach) && hit.transform.tag == "lowCabinet" && lowCabinetIsClosed == false)
{
RayHit = true;
cabinets = hit.collider.gameObject.GetComponent("Cabinets");
drawGUIClose = true;
if (Input.GetKeyDown("mouse 0"))
{
cabinets.TheLowCabinetCloses();
lowCabinetIsClosed = true;
drawGUIClose = false;
}
}
else
{
RayHit = false;
drawGUIOpen = false;
drawGUIClose = false;
}
}
function OnGUIOpen()
{
if (drawGUIOpen == true)
{
GUI.Box (Rect (Screen.width*0.5-51, 200, 102, 22), “Click to open”);
}
}
function OnGUIClose()
{
if (drawGUIClose == true)
{
GUI.Box (Rect (Screen.width*0.5-51, 200, 102, 22), “Click to close”);
}
}**
```
My shelf objects with tags and animations