Question by
Hopepie · Feb 12, 2017 at 05:51 PM ·
scripting problemassetstween
i want to drag and drop a reference to a component in editor so i can activate and deactivate them?
So i made a script for jumping between different controllers from different assets. All works fine except i cant referens a componenet by drag and drop.
Here's the compelete class have fun if you have use for it =) .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SmoothSwitch : MonoBehaviour {
//Made by Sami aka Hopepie
//Sorry for the bad english
//this script has 3 phases 2 where you disabel and enebel objects and a move and a rotate transform in between
//to use in switching between gamemodes etc. the middel phase with move and rotate is orgignila planed for a camera during transion into a car or simelar
//im using to jump between diffrent player/controller lika a car or fps player.
//you can use 2 sets of the script to jump back and forward. Have one of the script disabeld and switch between them as you go with the swiche on off feature.
//Activatt and deactivate stuff at start and end move something in between.
//To access components i have to code manually for objects drag and drop in editor
public enum localStates
{
stoped,
starting,
inTransit,
ending
}
[System.Serializable]
public struct moveJob
{
public Transform start;
public Transform end;
public Transform objectToMove;
}
[System.Serializable]
public struct rotatejob
{
public Transform start;
public Transform end;
public Transform objectToRotate;
}
public string Notes; //notes area so you can keep 2 script appart on the same object if you filp between them
public GameObject[] startActivateObjects;
public GameObject[] startDeactivateObjects;
//public Component[] startActivateComponents; //i want to drag and drop compenets but have not gotten it to work pls send me a mail if you sole it.
//public Component[] startDeactivateComponents;
public AnimationCurve moveCurve = AnimationCurve.Linear(0.0f, 0.0f, 1.0f, 1.0f);
public AnimationCurve rotateCurve = AnimationCurve.Linear(0.0f, 0.0f, 1.0f, 1.0f);
public float duration = 5.0f;
public moveJob[] moveObjects;
public rotatejob[] rotateObjects;
public GameObject[] endActivateObjects;
public GameObject[] endDeactivateObjects;
//public Component[] endActivateComponents;
//public Component[] endDeactivateComponents;
public localStates currentState = localStates.stoped;
float elapsedTime;
// Use this for initialization
void Start()
{
elapsedTime = 0.0f;
}
// Update is called once per frame
void Update()
{
//jump out if inactive
if (currentState == localStates.stoped)
return;
if (currentState == localStates.starting)
{
// make stuff here 1 time before move
//Activate objects
if (startActivateObjects != null && startActivateObjects.Length != 0)
{
for (int i = 0; i < startActivateObjects.Length; i++)
{
startActivateObjects[i].SetActive(true);
}
}
//deactivate Objects
if (startDeactivateObjects != null && startDeactivateObjects.Length != 0)
{
for (int i = 0; i < startDeactivateObjects.Length; i++)
{
startDeactivateObjects[i].SetActive(false);
}
}
//set to next state
currentState = localStates.inTransit;
}
if (currentState == localStates.inTransit)
{
//The move phase
elapsedTime += Time.deltaTime;
float progress = elapsedTime / duration;
//loop thru all objects to move if any and move them by cure
if (moveObjects != null && moveObjects.Length != 0)
{
for (int i = 0; i < moveObjects.Length; i++)
{
moveObjects[i].objectToMove.position = Vector3.Lerp(moveObjects[i].start.position, moveObjects[i].end.position, moveCurve.Evaluate(progress));
}
}
//loop thru all objects to rotate if any and rotate them by curve
if (rotateObjects != null && rotateObjects.Length != 0)
{
for (int i = 0; i < rotateObjects.Length; i++)
{
//Using lerp instead of slerp du to preformance and in combo with curve slerp is overkill
rotateObjects[i].objectToRotate.rotation = Quaternion.Lerp( rotateObjects[i].start.rotation, rotateObjects[i].end.rotation, rotateCurve.Evaluate(progress));
}
}
//check if move is done
if (elapsedTime >= duration)
{
currentState = localStates.ending;
}
}
if(currentState == localStates.ending)
{
currentState = localStates.stoped;
elapsedTime = 0.0f;
//Activate objects
if (endActivateObjects != null && endActivateObjects.Length != 0)
{
for (int i = 0; i < endActivateObjects.Length; i++)
{
endActivateObjects[i].SetActive(true);
}
}
//deactivate Objects
if (endDeactivateObjects != null && endDeactivateObjects.Length != 0)
{
for (int i = 0; i < endDeactivateObjects.Length; i++)
{
endDeactivateObjects[i].SetActive(false);
}
}
}
}
void FixedUpdate()
{
//only start if not running
if (currentState == localStates.stoped)
{
if (Input.GetKeyDown(KeyCode.Z))
{
currentState = localStates.starting;
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Changing a Prefab's Text component seems to be broken. 1 Answer
How to assign existing material to an object ? 1 Answer
unity keeps crashing when importing assets and when writing scripts 0 Answers
ParticleSystemMultiplier class missing a reference 2 Answers
Tanks game script SetHealthUI error 1 Answer