• 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 /
avatar image
Question by Chocolade · Jul 22, 2017 at 09:44 PM · c#scripting problemscript.

I have array of gameobject. How can i make a class/es with options for each gameobject in the array ?

T$$anonymous$$s is what i tried so far:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public struct SpinableObject
 {
     public Transform t;
     public float rotationSpeed;
     public float minSpeed;
     public float maxSpeed;
     public float speedRate;
     [HideInInspector]
     public bool slowDown;
 }
 
 public class SpinObject : MonoBehaviour
 {
     private bool slowDown = false;
     public float rotationSpeed;
     public float slowdownMax;
     public float slowdownMin;
     public GameObject[] objectsToRotate;
 
     // Use t$$anonymous$$s for initialization
     void Start ()
     {
         
     }
     
     // Update is called once per frame
     void Update ()
     {
         for (int i = 0; i < objectsToRotate.Length; i++)
         {
             SpinableObject spinableObject = objectsToRotate[i];
 
             if(spinableObject.rotationSpeed > spinableObject.maxSpeed)
                 spinableObject.slowDown = true;
             else if (spinableObject.rotationSpeed < spinableObject.minSpeed)
                 spinableObject.slowDown = false;
 
             spinableObject.rotationSpeed = (spinableObject.slowDown) ? spinableObject.rotationSpeed - 0.1f : spinableObject.rotationSpeed + 0.1f;
             spinableObject.t.Rotate(Vector3.forward, Time.deltaTime * spinableObject.rotationSpeed);
         }
     }
 }
 

But i'm getting error on:

 SpinableObject spinableObject = objectsToRotate[i];

The error is on the: objectsToRotate[i]

Cannot implicitly convert type 'UnityEngine.GameObject' to 'SpinableObject'

So first how can i fix the error ?

Second how can i make it instead using struct to use two classes:

A class with its own function to manage the rotation and another one to indicate whether the object should accelerate or decelerate

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by UnityCoach · Jul 22, 2017 at 11:45 PM

I would rather have SpinableObject as a MonoBehaviour on every object you want to rotate. Then, if you want to sync' them, you can use some kind of Manager to w$$anonymous$$ch they can subscribe to events, or receive method calls.

Comment
Chocolade

People who like this

1 Show 3 · Share
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
avatar image Chocolade · Jul 22, 2017 at 11:50 PM 0
Share

Can you show me please some example or how to start doing it ?

This is the code i'm using now:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class SpinableObject
 {
     [SerializeField]
     private Transform t;
     [SerializeField]
     private float rotationSpeed;
     [SerializeField]
     private float minSpeed;
     [SerializeField]
     private float maxSpeed;
     [SerializeField]
     private float speedRate;
     private bool slowDown;
 
     public void Rotate()
     {
         if (rotationSpeed > maxSpeed)
             slowDown = true;
         else if (rotationSpeed < minSpeed)
             slowDown = false;
 
         rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
         t.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
     }
 }
 public class SpinObject : MonoBehaviour
 {
     private bool slowDown = false;
     public float rotationSpeed;
     public float slowdownMax;
     public float slowdownMin;
     public SpinableObject[] objectsToRotate;
 
     // Use this for initialization
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         for (int i = 0; i < objectsToRotate.Length; i++)
         {
             objectsToRotate[i].Rotate();
         }
     }
 }
     
 
 
avatar image Hellium · Jul 23, 2017 at 08:59 AM 0
Share

@UnityCoach : Can you explain why a MonoBehaviour would be preferable instead a "standard" class ? I've always thought MonoBehaviour adds an additional "heavy" layer, which can be avoided here.

avatar image UnityCoach Hellium · Jul 23, 2017 at 06:17 PM 0
Share

Well, this is paradigm opinion really, I usually find it more natural when an object controls itself, yet sync'ing with others with some Manager class when needed, rather than being controlled by another. If you have thousands of objects, then maybe there's a difference, but you can't really tell for sure until you benchmark this. MonoBehaviours are quite optimised, you can turn them off when you don't need them, etc. Also, storing all the objects in a big List and go through that list every Update cycle, this is what the Engine does.

avatar image

Answer by Hellium · Jul 22, 2017 at 10:27 PM

 public GameObject[] objectsToRotate

must be

  public SpinableObject[] objectsToRotate
Comment

People who like this

0 Show 2 · Share
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
avatar image Chocolade · Jul 22, 2017 at 10:43 PM 0
Share

This is working now. but i'm not sure if it's a good way. This is what i ended with so far:

But i guess making a loop(FOR) inside the Update is a bad idea ? (as the function is called every frame and the for loop would be activated every frame lowering the fps drastically). Maybe there should be some IF there or something else ?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class SpinableObject
 {
     [SerializeField]
     private Transform t;
     [SerializeField]
     private float rotationSpeed;
     [SerializeField]
     private float minSpeed;
     [SerializeField]
     private float maxSpeed;
     [SerializeField]
     private float speedRate;
     private bool slowDown;
 
     public void Rotate()
     {
         if (rotationSpeed > maxSpeed)
             slowDown = true;
         else if (rotationSpeed < minSpeed)
             slowDown = false;
 
         rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
         t.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
     }
 }
     public class SpinObject : MonoBehaviour
     {
         private bool slowDown = false;
         public float rotationSpeed;
         public float slowdownMax;
         public float slowdownMin;
         public SpinableObject[] objectsToRotate;
 
         // Use this for initialization
         void Start()
         {
 
         }
 
         // Update is called once per frame
         void Update()
         {
             for (int i = 0; i < objectsToRotate.Length; i++)
             {
                 objectsToRotate[i].Rotate();
             }
         }
     }
avatar image Hellium Chocolade · Jul 23, 2017 at 08:55 AM 0
Share

Don't worry, a simple for loop won't make your FPS drop. Except if you have thousand objects in your array.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

374 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can i characters in triangle formation with equal spaces between each character ? 0 Answers

How can i avoid any gameobject overlapping when instantiate the gameobjects ? 0 Answers

How can i color the grid 4 walls and add spaces between cubes ? 0 Answers

How can i attach a script to many gameobject in array ? 1 Answer

Multiple gameobjects but only one is screen wrapping. 0 Answers


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