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 jhunglow · May 24, 2015 at 02:51 PM · booleanbooleans

GetComponent not working in C#

I am trying to reference another script for a bool called move. Here is script #1

using UnityEngine; using System.Collections;

public class Crate_Forward : MonoBehaviour {

 public float moveSpeed = 10f;
 public bool move; 
 
 // Use this for initialization
 void Start () {
     move = false;
     
 }
 
 // Update is called once per frame
 void Update () {
     if (move == true) {
         if (Input.GetKey (KeyCode.Keypad8)) {
             
             transform.Translate (Vector3.up * moveSpeed * Time.deltaTime);
         }
     }
 }

}

In Script #2 i put this to reference the bool move: //the object script #1 is located in is called crate

if (GameObject.Find("crate").GetComponent().move)

This does not seem to work as Unity Editor does not recognize the script Crate_Forward.

*These scripts are located in different objects

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TwinPrime · May 24, 2015 at 03:00 PM

in script num 2 add this public variable ;

 public Crate_Forward script;

then grap and drag your scipt in it. then in start func;

 script = gameObject.GetComponent<Crate_Forward>;

then in your update func;

 script.move();


Hope it helps

Comment
Add comment · Show 1 · 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 jhunglow · May 24, 2015 at 03:19 PM 0
Share

Thanks but i still get this error in the console message.

Assets/Standard Assets/Characters/RollerBall/Scripts/Ball.cs(28,24): error CS0246: The type or namespace name `Crate_Forward' could not be found. Are you missing a using directive or an assembly reference?

When i typed public Crate_Forward script; It made me insert a gameObject not a script.

avatar image
0

Answer by DiegoSLTS · May 24, 2015 at 03:27 PM

GetComponent works in C#, the problem in your code is somewhere else. I just copied your "Crate_Forwad" class code and setup an object called "crate" and I can write this line in another script without any compiler error:

 Crate_Forward script = GameObject.Find("crate").GetComponent<Crate_Forward> ();

Make sure your class "Crate_Forward" is defined inside a file named "Crate_Forward.cs", the file name and the class inside it must always match.

Also, make sure there are no other compiler errors in your code.

Comment
Add comment · Show 7 · 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 jhunglow · May 24, 2015 at 03:54 PM 0
Share

It still says Crate_Forward cannot be found in the current text. Both the class and script name are the same as well. "Crate_Forward"

here is the error

Assets/Standard Assets/Characters/RollerBall/Scripts/Ball.cs(29,17): error CS0246: The type or namespace name `Crate_Forward' could not be found. Are you missing a using directive or an assembly reference?

avatar image DiegoSLTS · May 24, 2015 at 05:13 PM 0
Share

Just to be clear, when you select the Crate_Forward file in your project window inside Unity it displays "Crate_Forward.cs" at the bottom?

It's too weird that it's not working. As I said, I just copy pasted your code and Unity recognices the class. $$anonymous$$aybe you have other scripts conflicting with your Crate_Forward script, I see you have your Ball asset inside the Standard Assets subfolder, so maybe there.

Or maybe your file has some invalid characters on it, did you copy past code from somewhere? Try coping your Crate_Forward code in some temporary place (like a text file outside the project) and recreate the file in Unity, writing the code manually.

Also,can you share a screenshot of your Project window in Unity?

avatar image jhunglow · May 24, 2015 at 06:02 PM 0
Share

alt text

here are screenshots of the two file locations. I tried recreating the script manually as well but it had the same outcome.

ballcs-stript-location.png (289.5 kB)
forward-crate-file-location.png (294.4 kB)
avatar image jhunglow · May 24, 2015 at 06:05 PM 0
Share

alt text

Here are the locations of the two files. I also tried to manually recreate the "Forward_Crate.cs" file but it still did not work.

ballcs-stript-location.png (289.5 kB)
forward-crate-file-location.png (294.4 kB)
avatar image DiegoSLTS · May 24, 2015 at 08:17 PM 0
Share

If you comment the line in Ball.cs giving the error, the code compiles? Everything else works?

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can i reverse all the booleans in a method? 2 Answers

When click on a button, play an animator animations through booleans 3 Answers

How do you Manage Multiple Boolean Better, I know I'm supposed to use a foreach but I have no idea how that works 1 Answer

Project Not working after 1 day and no changes. 1 Answer

How can I make this work when the booleans are equal? 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