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 Alanj2007 · Sep 22, 2013 at 02:47 PM · expression

Something wrong with this script?

I got an error says : Assets/ActiveGenerator.js(10,66): BCE0049: Expression 'self.GeneratorGUI.GetComponent(GeneratorCountNumbers).CountGenerator' cannot be assigned to.

These are my scripts. Script A : This script called GeneratorCountNumbers :

 //Script A
 
 var CountGenerator : int = 0;
 
 function OnGUI()
 {
     if(CountGenerator == 1)
     {
         GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "You need 2 more generator to activate the gate");
     }
     if(CountGenerator == 2)
     {
         GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "You need 1 more generator to activate the gate");
     }
     if(CountGenerator == 3)
     {
         GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "Gate is now activated");
     }
 }

Script B : This script called ActiveGenerator

 //Script B
 
 var message : boolean = false;
 var GeneratorGUI = GameObject;
 
 function OnTriggerStay(other : Collider)
 {
     if(Input.GetButton("e"))
     {
         GeneratorGUI.GetComponent(GeneratorCountNumbers).CountGenerator++;
     }
 }
 
 function OnTriggerEnter (other : Collider){
     if (other.gameObject.tag == "Player") {
         message = true;
     }
 }
      
 function OnTriggerExit (other : Collider){
     if (other.gameObject.tag == "Player") {
         message = false;
     }
 }
 
 function OnGUI(){
     if(message){
         GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "Press E to activate generator");
     }
 }

Errors at GetComponent in Script B Gameobject that attached with Script A called "GeneratorGUI"

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

3 Replies

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

Answer by Bunny83 · Sep 22, 2013 at 03:21 PM

This line:

     var GeneratorGUI = GameObject;

does not declare a variable of type GameObject but a variable of type System.Type which holds the type information of the GameObject class.

You need to declare it like this:

     var GeneratorGUI : GameObject;

And you need to assign a gameobject to the variable before you use it.

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 Alanj2007 · Sep 22, 2013 at 03:23 PM 0
Share

Oh god... are you kidding me? ... That's why my script getting error! Thanks..

I'm too stupid. I didn't notice that xD

avatar image
0

Answer by Unique-Games · Sep 22, 2013 at 03:08 PM

I've ever used .GetComponent(), so i wont be able to help as much as i would like but what i do instead is in ScriptA have your variable as a static var (I.e. static var CountGenerator : int = 0;), then in ScriptB instead of GeneratorGUI... use ScriptA.CountGenerator++; (Replace ScriptA with the name of your first script (which contains CountGenerator). Hope this helps :) - UniqueGames

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 Alanj2007 · Sep 22, 2013 at 03:19 PM 0
Share

Is it gonna be like this

SCRIPT A

 //Script A
 
 static var CountGenerator : int = 0;
 
 function OnGUI()
 {
     if(CountGenerator == 1)
     {
         GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "You need 2 more generator to activate the gate");
     }
     if(CountGenerator == 2)
     {
         GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "You need 1 more generator to activate the gate");
     }
     if(CountGenerator == 3)
     {
         GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "Gate is now activated");
     }
 }

and SCRIPT B

 //Script B
 
 var message : boolean = false;
 var GeneratorGUI = GameObject;
 
 function OnTriggerStay(other : Collider)
 {
     if(Input.GetButton("e"))
     {
         GeneratorGUI.GetComponent(ScriptA).CountGenerator++;
     }
 }
 
 function OnTriggerEnter (other : Collider){
     if (other.gameObject.tag == "Player") {
         message = true;
     }
 }
      
 function OnTriggerExit (other : Collider){
     if (other.gameObject.tag == "Player") {
         message = false;
     }
 }
 
 function OnGUI(){
     if(message){
         GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "Press E to activate generator");
     }
 }

It gives me an error

Assets/ActiveGenerator.js(10,18): BCE0005: $$anonymous$$ identifier: 'ScriptA'.

avatar image
0

Answer by YoungDeveloper · Sep 22, 2013 at 03:14 PM

     //Script B
      
     var message : boolean = false;
     var thatObject = GameObject;
 
     var script_A : ScriptA;
      
     function OnTriggerStay(other : Collider)
     {
     if(Input.GetButton("e"))
     {
     script_A = thatObject.GetComponent("GeneratorGUI");
 
     script_A.Action();
     }
     }
      
     function OnTriggerEnter (other : Collider){
     if (other.gameObject.tag == "Player") {
     message = true;
     }
     }
      
     function OnTriggerExit (other : Collider){
     if (other.gameObject.tag == "Player") {
     message = false;
     }
     }
      
     function OnGUI(){
     if(message){
     GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "Press E to activate generator");
     }
     }
 
 //////////////////////////////////
 //Script A
 
 var CountGenerator : int = 0;
 
 function Action(){
 CountGenerator++;
 }
 
 function OnGUI()
 {
 if(CountGenerator == 1)
 {
 GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "You need 2 more generator to activate the gate");
 }
 if(CountGenerator == 2)
 {
 GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "You need 1 more generator to activate the gate");
 }
 if(CountGenerator == 3)
 {
 GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "Gate is now activated");
 }
 }
Comment
Add comment · 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

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

19 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

Related Questions

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

How to import the object from server to unity 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Material doesn't have a color property '_Color' 4 Answers

Setting Scroll View Width GUILayout 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