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 TripleTea · Jan 10, 2016 at 03:07 AM · c#unity 5scripting problemscripting beginnervariables

C# Unity dot syntax exercise correct solution?

I'm a noob in C# and Unity aspect so please have mercy.

I'm doing an exercise on dot syntax which requires me to have two GameObjects and two scripts.

First script:

 using UnityEngine;
 using System.Collections;
 
 public class Spinner : MonoBehaviour
 {
     public void SpinLeft()
     {
         transform.Rotate(0,0,60 * Time.deltaTime);
     }
 
     public void SpinRight()
     {
         transform.Rotate(0,0,-60 * Time.deltaTime);
     }
 }

Second script:

 using UnityEngine;
 using System.Collections;
 
 public class LearningScript : MonoBehaviour
 {
     GameObject capsuleGO;
     Spinner cubeComp;
 
     void Start()
     {
         capsuleGO = GameObject.Find("Capsule");
         Debug.Log(capsuleGO);
         cubeComp = GameObject.Find("Cube").GetComponent<Spinner>();
         Debug.Log(cubeComp);
     }
 
     void Update()
     {
         if(Input.GetKey(KeyCode.LeftArrow))
         {
             capsuleGO.GetComponent<Spinner>().SpinLeft();
         }
 
         if(Input.GetKey(KeyCode.RightArrow))
         {
             capsuleGO.GetComponent<Spinner>().SpinRight();
         }
 
         if(Input.GetKey(KeyCode.UpArrow))
         {
             cubeComp.SpinLeft();
         }
 
         if(Input.GetKey(KeyCode.DownArrow))
         {
             cubeComp.SpinRight();
         }
     }
 }

On the Second script I was asked to make this portion of it:

 if(Input.GetKey(KeyCode.LeftArrow))
         {
             capsuleGO.GetComponent<Spinner>().SpinLeft();
         }

and turn it into something like this ↓ where I didn't have to write the extra "GetComponent..."

 if(Input.GetKey(KeyCode.UpArrow))
         {
             cubeComp.SpinLeft();
         }

Exercise Question: is there a way to code it so that it doesn't have to access "Spinner" by assigning a new variable called "capsuleComp" that stores a reference to "Spinner" Component instead of using "capsuleGO"?

My solution to this was this code whereby I made an exact "Spinner" script and named it "Spinner2" and attached it to the same two GameObjects then changed the code to ↓:

 using UnityEngine;
 using System.Collections;
 
 public class LearningScript : MonoBehaviour
 {
     Spinner cubeComp;
     Spinner2 capsuleComp;
 
     void Start()
     {
         capsuleComp = GameObject.Find ("Capsule").GetComponent <Spinner2>();
         Debug.Log(capsuleComp);
         cubeComp = GameObject.Find("Cube").GetComponent<Spinner>();
         Debug.Log(cubeComp);
     }
 
     void Update()
     {
         if(Input.GetKey(KeyCode.LeftArrow))
         {
             capsuleComp.SpinLeft();
         }
 
         if(Input.GetKey(KeyCode.RightArrow))
         {
             capsuleComp.SpinRight();
         }
 
         if(Input.GetKey(KeyCode.UpArrow))
         {
             cubeComp.SpinLeft();
         }
 
         if(Input.GetKey(KeyCode.DownArrow))
         {
             cubeComp.SpinRight();
         }
     }
 }

My question: Is this the right way to do it in terms of coding (functionality wise and efficiency wise)?

I feel like I didn't understand the main concept of the exercise but managed to find a solution somehow...Any pointers would be much appreciated since I'm since in my learning phase of C# and Unity. Thanks in advance and sorry for the long post...

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by LazyElephant · Jan 10, 2016 at 08:33 AM

I may be misunderstanding your solution, but it sounds like you directly copied the spinner script so that both game objects now have a spinner and a spinner2? This is bad design, since you have two classes doing the exact same thing.

It seems that the exercise was more to see if you understood how to access a component using the Find() and getComponent<T>() methods so that you could reference it in your script. It is a good idea to do this in the start() method of your scripts once for each object you have to find and store it in a variable in your class. It is more efficient and, if you have to call these types of search functions very often in your update() methods, you run the risk of slowing down your game.

For the solution, one thing you need to understand is that, even though you only wrote the script once, each game object has it's own copy of it when you add it as a component. So to get the spinner component for the CapsuleGO, you simply create another Spinner variable in your class and store the CapsuleGO spinner in it in your Start() function. This would then allow you to reference it with variable.function().

 using UnityEngine;
 using System.Collections;
 
 public class LearningScript : MonoBehaviour
 {
  Spinner cubeComp;
  Spinner capsuleComp;

 
  void Start()
  {
      capsuleComp = GameObject.Find("Capsule").GetComponent<Spinner>();
      Debug.Log(capsuleComp);
      cubeComp = GameObject.Find("Cube").GetComponent<Spinner>();
      Debug.Log(cubeComp);
  }
 
  void Update()
  {
      if(Input.GetKey(KeyCode.LeftArrow))
      {
          capsuleComp.SpinLeft();
      }
 
      if(Input.GetKey(KeyCode.RightArrow))
      {
          capsuleComp.SpinRight();
      }
 
      if(Input.GetKey(KeyCode.UpArrow))
      {
          cubeComp.SpinLeft();
      }
 
      if(Input.GetKey(KeyCode.DownArrow))
      {
          cubeComp.SpinRight();
      }
    }
 }

The fact that you made a workable answer is good. It means you can think around a problem. With more experience and language specific knowledge, you'll be able to create better solutions in the future.

Comment
Add comment · 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 TripleTea · Jan 10, 2016 at 06:03 PM 0
Share

Nope, you did not misunderstood my solution. That's exactly what I did LOL...Yes, I felt it was bad practice design that's why I felt I had to find the proper solution to it (I tested it and it ran perfectly!) and you handed that very solution to me. Thank you so much for taking the time to answer my question! I kept thinking and trying but I just couldn't figure it out!

lol...I was VERY glad when I found the workaround solution but something just didn't feel right about it.

I have another question though...How was it possible to assign "Spinner" to two different variables? Is it because "Spinner" is just a function/method?? I thought that wasn't possible and would definitely give me an error.

avatar image LazyElephant TripleTea · Jan 10, 2016 at 06:50 PM 0
Share

The spinner script you wrote was just the class definition. You can have as many instances of a class as you like. By adding the script to both the cube and capsule, you instantiated two separate instances of the class, so all you needed were the two variables to store references to them.

avatar image TripleTea LazyElephant · Jan 10, 2016 at 08:06 PM 0
Share

Okay...I think I need to read more and drill the different definitions to my head...

Thanks again!

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

65 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

Related Questions

how do i keep an item destroyed when i change the scene c# 0 Answers

Adding a cost reduction to my purchasable items 1 Answer

Dreamlo Leaderbord issue 0 Answers

Having trouble deleting objects from a list after they reach a certain scale. 0 Answers

Adding a Highscore to game 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