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 Feretro · May 05, 2018 at 11:26 AM · scripting problemeditorobjectvariablespassing

Dragging an object to variable: Problems in the editor!

Hello everybody! :-)

I am pretty new in this world (2 weeks o so!) (never used Unity and C# ... or any language program since 1986, basic)

I am learning and testing, so I needed to pass variables value from a script to another, and so I found this:

https://answers.unity.com/questions/494037/passing-variables-to-another-script.html

Now .. my problem is the:

 [...] Then, in the editor, simply drag the object that has the script on it over to the variable propertie in the inspector view. [...]

part of the answer.

As i Drag object in the editor: alt text

Dati Maiale" is working correctly (I see maiale (maiale)but when I hit RUN ...

"Dati Maiale" disappears and so ... I get errors (Null reference).

alt text

"Dati Maiale" is working correctly (I see maiale (maiale))

As you can see ... there is "NONE" now.

When I stop program ... things come to normality. Of course if I manually drag object during "Playing" it works ... but only once! (Until I stop and run again).

How can I set correctly that object in the editor? I'd like to fix that "maiale" object dragging it in place once and forever! :D

And here there are my scripts:

First one, is the one i want to get variables:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class maiale : MonoBehaviour
 {
 
     public Rigidbody2D maialeRB;
     AudioSource suonoCammina;
     Animator myAnimator;
 
     public float velocità = 7;
     public bool aSinistra;
     public Vector3 scalarePorco;
 
     // Use this for initialization
     void Start()
     {
 
         maialeRB = GetComponent<Rigidbody2D>();
         suonoCammina = GetComponent<AudioSource>();
         myAnimator = GetComponent<Animator>();
 
 
         aSinistra = true;
     }
 
     // Update is called once per frame
 
     private void FixedUpdate()
     {
 
         float orizzontale = Input.GetAxis("Horizontal");
         GestioneMovimenti(orizzontale);
         GiraIlPorco(orizzontale);
         SuonoPorcoCammina(orizzontale);
 
     }
 
 
     void Update()
     {
 
     }
 
     void GestioneMovimenti(float orizzontale)
     {
 
         maialeRB.velocity = new Vector2(orizzontale * velocità, maialeRB.velocity.y);
 
     }
 
     void SuonoPorcoCammina (float orizzontale) {
 
         if (orizzontale < 0 && !suonoCammina.isPlaying || orizzontale > 0 && !suonoCammina.isPlaying)
         {
 
             suonoCammina.Play();
             myAnimator.SetBool("corre", true);
 
 
         }
 
         if (Mathf.Abs(orizzontale) < 0.001)
         {
 
             suonoCammina.Pause();
             myAnimator.SetBool("corre", false);
 
 
         }
 
         }
 
 
     void GiraIlPorco(float orizzontale)
     {
 
         if (aSinistra && orizzontale > 0 || !aSinistra && orizzontale < 0)
         {
 
             aSinistra = !aSinistra;
 
              
             scalarePorco = transform.localScale;
             scalarePorco.x = scalarePorco.x * -1;
             transform.localScale = scalarePorco;
 
         }
 
 
     }
 
 }


And this is the SECOND script .. in witch i need to "know" the first one variable values:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class pecora : MonoBehaviour {
 
     public maiale datiMaiale;
 
 
     // Use this for initialization
     void Start () {
 
         datiMaiale = GetComponent<maiale>();
 
        
     }
     
     // Update is called once per frame
     void Update () {
 
 
         Debug.Log(datiMaiale.velocità);
 
 
     }
 
 
     void GiraLaPecora () {
 
         Vector3 scalarePecora = transform.localScale;
 
         Debug.Log(datiMaiale.scalarePorco.x);
 
         if (datiMaiale.scalarePorco.x <= 0) {
 
             scalarePecora.x = scalarePecora.x * -1;
             transform.localScale = scalarePecora;
 
         }
 
         if (datiMaiale.scalarePorco.x >0) {
 
             scalarePecora.x = scalarePecora.x * -1;
             transform.localScale = scalarePecora;
 
 
         }
 
     }
 }
schermata-2018-05-05-alle-130421.png (30.0 kB)
schermata-2018-05-05-alle-130828-1.png (23.1 kB)
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 KittenSnipes · May 06, 2018 at 10:31 AM

@Feretro

This is because you misunderstood. Unity only allows you to drag a gameobject with that script attached in to the open property spot in the editor. The script itself can not be used in that way. Especially because the script itself does not contain a rigid body. You are grabbing null components that must be set so it would make sense to add that script to an object and give it the needed properties using the same gameobject. Your scripts are more like components and you are only allowed to use another objects component to do anything. If you drag the script itself just think of it as giving a null or empty component.

Comment
Add comment · Show 2 · 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 Feretro · May 06, 2018 at 03:34 PM 0
Share

HI $$anonymous$$S! And thanks for your reply! :-) This "world" is still obscure to me! That's why I am following video-tutorials and forums! :-)

I found very useful to access to other script variables by using (as instance)

"otherScriptValues.ExampleBoolVariable"

How do you suggest me to share variable values between two scripts?

Thanks and sorry if $$anonymous$$y questions seem to be "strange" or messy .... but I am starting from zero! :P

avatar image KittenSnipes Feretro · May 07, 2018 at 02:05 AM 0
Share

@Feretro

Ill link you to a small tutorial video so you understand a bit better. So basically what I do in the video is I have 2 gameobjects with 2 different scripts. One script just gets a rigidbody component. The second script has a rigidbody and uses the other gameobjects scripts as a reference to get its rigidbody and then sets it as the rigidbody it uses in its script. Then it reacts. That all there is to it. Hope it helps you understand what I mean now.

Helpful Tutorial Video I Explained

avatar image
0

Answer by Feretro · May 08, 2018 at 09:43 AM

Wops! Sorry!!! I Did not noticed your reply. And sorry for my bad english! I can't explain myself as I'd desire! I am not still getting the concept of "RigidBody". I followed your tutorial and yes, It works. I can access to my "First Script" PUBLIC variables from my "Second Script".

But ... I am getting access anyways without involving rigidbody and now I am confused! :P

if in the second script I do like this:

     public FirstScript dataFirstScript;

.. i can have access to all public "First Script" data by dragging in the Editor by "First Character" into second's "dataFirstScript" field.

My inexperience, ignorance and poor english skills may cause misunderstandings! :D Thanks for your video!

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

226 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 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 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 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

scripting problems please help 0 Answers

I want Script To Move a object from a different object (In four Different directions,Randomly) . 0 Answers

Text object updating on everything else except for variable 0 Answers

Networking Unity, I need to pass a simple variable value between Client and Server. 1 Answer

Display Variable with UI Text 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