Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges