Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by c6y · Dec 26, 2013 at 11:39 AM · guicomponent

Cannot access variable of another object's script

In my GUI script I want to set a variable's value to true on another object's script. But it's not working ... :/

Thanks for any help!

The error message:

error CS1061: Type UnityEngine.Object' does not contain a definition for doRotateL' and no extension method doRotateL' of type UnityEngine.Object' could be found (are you missing a using directive or an assembly reference?)

The GUI Script:

 using UnityEngine;
 using System.Collections;
 
 public class GUITest : MonoBehaviour {
 
     public GUISkin mySkin;
     public GameObject bodies;
     private Object rotatorScript;
 
     void Start() {
     }
 
     void OnGUI() {
 
         GUI.skin = mySkin;
 
         GUILayout.BeginArea (new Rect (Screen.width*2/72, Screen.height/3, Screen.width*22/72, Screen.height/3));
         
         if(GUILayout.Button ("MoveBodyLeft", "button")) {
 
             rotatorScript = bodies.GetComponent("ClickRotator01");
             rotatorScript.doRotateL = true;
         }
         
         GUILayout.EndArea ();
     }
 }

The other Object's script:

 using UnityEngine;
 using System.Collections;
 
 public class ClickRotator01 : MonoBehaviour {
 
 public float rotationStepAngle = 30.0F;
 public float timer = 10.0F;
 
 private float rotationToAngle;
 public bool doRotateL = true;
 
      void Start () {
         float rotationToAngle = rotationStepAngle;
     }
 
     void Update () {
         Rotator();
     }
     
     void Rotator()
     {
 
         if(doRotateL){
             
             Quaternion newRotation = Quaternion.AngleAxis(rotationToAngle, Vector3.up);
             transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * timer);
 
             if (transform.rotation == newRotation){
                 
                 transform.rotation = newRotation;
 
                 doRotateL = false;
                 rotationToAngle += rotationStepAngle;
             }
         }
     }
 }
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 KellyThomas · Dec 26, 2013 at 12:33 PM

  1. When your variable is of type Object you can only access method of Object.

  2. When you use the GetComponent(string type) overload of `GetComponent()` it will return a Component, however the generic form will return the specified type..

I suggest the following changes to GUITest:

 //line 8 replacement:
 //private Object rotatorScript;
 private ClickRotator01 rotatorScript;


 //line 21 replacement:
 //rotatorScript = bodies.GetComponent("ClickRotator01");
 rotatorScript = bodies.GetComponent<ClickRotator01>();

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 c6y · Dec 27, 2013 at 09:46 AM -1
Share

Thanks so much! @$$anonymous$$elly$$anonymous$$

Just for the record, one additional $$anonymous$$or edit was necessary to the change you made to line 5:

 //line 21 replacement:
 rotatorScript = bodies.GetComponent<ClickRotator01>();
avatar image Benproductions1 c6y · Dec 27, 2013 at 09:47 AM 0
Share

Please post comments as comments not as answers!

avatar image KellyThomas · Dec 27, 2013 at 10:24 AM 0
Share

Thanks @c6y, correction made to answer above.

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

20 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Create a Button Scrollview 0 Answers

float error with screen.width/height 0 Answers

enabling and disabling hingeJoint in Unity 4.3 2 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