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:
Dati Maiale" is working correctly (I see maiale (maiale)but when I hit RUN ...
"Dati Maiale" disappears and so ... I get errors (Null reference).
"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;
}
}
}
Answer by KittenSnipes · May 06, 2018 at 10:31 AM
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.
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
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.
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!
Your answer
Follow this Question
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