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
1
Question by LAlejandroRS · Aug 22, 2013 at 09:23 PM · accessing from any script

Problema comunicar script

Buenas tardes amigos, tengo una duda, realice un script el cual contiene la energía del personaje:

public int energia = 100;

en otro script contacto esta variable para irla reduciendo al tocar el fuego, osea, este script se lo puse al fuego:

public string mensaje; public Energia energiaPersonaje;

void Start(){ energiaPersonaje = new Energia(); mensaje = energiaPersonaje.energia.ToString(); }

void OnTriggerStay(Collider hit){ if(hit.gameObject.tag == "Player"){ energiaPersonaje.energia = energiaPersonaje.energia - 1; mensaje = energiaPersonaje.energia.ToString(); if(energiaPersonaje.energia == 0){ mensaje = "Estas muerto"; } } }

void OnTriggerExit(Collider hit){ if(hit.gameObject.tag == "Player"){ mensaje = energiaPersonaje.energia.ToString(); } }

void OnGUI(){ GUI.Label(new Rect(50,10,1500,50), "Vida: "); GUI.Label(new Rect(90,10,1500, 50), mensaje); }

en otro script, el cual se lo puse a un cubo, estoy tratando que al entrar en el, el personaje se cure:

private Energia energiaPersonaje; private Fuego fuegoEscena;//llamo al script 2 para usar su mensaje

void Start(){ energiaPersonaje = new Energia(); fuegoEscena = new Fuego(); }

void OnTriggerStay(Collider hit){ if(hit.gameObject.tag == "Player"){ energiaPersonaje.energia = energiaPersonaje.energia + 1; fuegoEscena.mensaje = energiaPersonaje.energia.ToString(); if(energiaPersonaje.energia == 100){ fuegoEscena.mensaje = "Te recuperaste!"; } } }

void OnTriggerExit(Collider hit){ if(hit.gameObject.tag == "Player"){ fuegoEscena.mensaje = energiaPersonaje.energia.ToString(); } }

el script 3 no me funciona como deseo, el personaje no se recupera de su energia, que puede estar mal?, gracias(:

Comment
Add comment · Show 2
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 Slobdell · Aug 22, 2013 at 11:11 PM 0
Share

Por favor cuando vas a poner código aquí, hay un boton arriba que tiene muchos 010101. Es para entrar código, y es mas facil para leer. Estas seguro que tu personaje tiene el tag "Player"?

avatar image Benproductions1 · Aug 23, 2013 at 03:07 AM 0
Share

Please format your code.

If you don't know how, watch the tutorial video on the right :)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by churi24 · Nov 13, 2013 at 04:58 PM

     public int energia = 100;
     
     //en otro script contacto esta variable para irla reduciendo //al tocar el fuego, osea, este script se lo puse al fuego:
     
     public string mensaje; 
     public Energia energiaPersonaje;
     
     void Start(){ 
         energiaPersonaje = new Energia(); 
         mensaje = energiaPersonaje.energia.ToString(); 
     }
     
     void OnTriggerStay(Collider hit){ 
         if(hit.gameObject.tag == "Player"){ 
             energiaPersonaje.energia = energiaPersonaje.energia - 1; 
             mensaje = energiaPersonaje.energia.ToString(); 
             if(energiaPersonaje.energia == 0){ 
                 mensaje = "Estas muerto"; 
             } 
         } 
     }
     
     void OnTriggerExit(Collider hit){ 
         if(hit.gameObject.tag == "Player"){ 
             mensaje = energiaPersonaje.energia.ToString(); 
         } 
     }
     
     void OnGUI(){ 
         GUI.Label(new Rect(50,10,1500,50), "Vida: "); 
         GUI.Label(new Rect(90,10,1500, 50), mensaje); 
     }
     
     //en otro script, el cual se lo puse a un cubo, estoy //tratando que al entrar en el, el personaje se cure:
     
     private Energia energiaPersonaje; 
     private Fuego fuegoEscena;//llamo al script 2 para usar su mensaje
     
     void Start(){ 
         energiaPersonaje = new Energia(); 
         fuegoEscena = new Fuego(); 
     }
     
     void OnTriggerStay(Collider hit){ 
         if(hit.gameObject.tag == "Player"){ 
             energiaPersonaje.energia = energiaPersonaje.energia + 1; 
             fuegoEscena.mensaje = energiaPersonaje.energia.ToString(); 
             if(energiaPersonaje.energia == 100){ 
                 fuegoEscena.mensaje = "Te recuperaste!"; 
             } 
         } 
     }
     
     void OnTriggerExit(Collider hit){ 
         if(hit.gameObject.tag == "Player"){ 
             fuegoEscena.mensaje = energiaPersonaje.energia.ToString(); 
         }
     }

Ese es tu codigo... lo que veo es que usaste new Energia(); Te vas a comunicar con energia pero te va a inicializar los atributos. En vez de usar tantas variables statics, que te van a limitar la programacion, deberias usar algo como energiaPersonaje = GetComponent(); Quita todos los statics que pusiste para arreglar eso, borra el new Energia(); y reemplazalo por lo que te mencione. El GetComponent lo podes usar siempre y cuando el script este en el gameObject de tu personaje. Si esta en otra ubicacion lo vas a tener que buscar con un Find, y si haces esto, hacelo en el evento Awake por motivos de performance.

Saludos!

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
avatar image
0

Answer by LAlejandroRS · Aug 23, 2013 at 04:42 PM

Listo amigos lo tendré en cuenta, gracias por sus respuestas, ya solucione el problema, simplemente tenia que usar las variables como static para poder manipularlas de script en script, buen dia (:

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

18 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

Related Questions

How to build Unity video player 1 Answer

Character Animation Issues 1 Answer

setting array elements mid-game in an array created/held by a different script 1 Answer

Problem accessing C# from Javascript 1 Answer

More elegant use of struct and class 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