- Home /
transferring variables between scripts c#
ok so i have build a tornado and a building that can be destroyed by it. the problem im having right now is that the building gets destroyed only if its hit with a certian amount of force from flying objects already in the tornado. the building is kineomatic so it wont move. the tornado can pick up objects with the tag of "Untagged". my house is marked other wise so it wont initially be picked up by the tornado. my house uses a destructable script so that when it is hit with a certian amount of force it is switched with a shattered version of itself. all the shattered pices are marked as untagged so then they will beguin flying around the tornado. i want to be able to make the tornado so if it has a certian amount of pull force. so right now my destructible script activates if the house is hit with a certian amount of force from other objects. but i also want it to activate if the tornado has a certian amount of pull force. here is the destructable script that goes on the house:
public GameObject debrisPrefab; public float strength; private bool isCloned;
void OnMouseDown() {
DestroyMe ();
}
void OnCollisionEnter(Collision collision ) {
if (collision.relativeVelocity.magnitude > strength) {
DestroyMe ();
}
}
void DestroyMe () {
if (debrisPrefab && !isCloned) {
Instantiate (debrisPrefab, transform.position, transform.rotation);
}
isCloned = true;
Destroy (gameObject);
}
}
here is the script that goes on my tornado and makes it pull objects:
private GameObject PullOBJ; public float PullSpeed; public float objRotationSpeed; public float rotation; public void OnTriggerStay (Collider coll) { if (coll.gameObject.tag == "Untagged") { PullOBJ = coll.gameObject;
PullOBJ.transform.position = Vector3.MoveTowards (PullOBJ.transform.position, this.transform.position, PullSpeed * Time.deltaTime);
PullOBJ.transform.RotateAround (transform.position, Vector3.up, Time.deltaTime * rotation);
PullOBJ.transform.Rotate (Vector3.left, 45 * Time.deltaTime * objRotationSpeed);
}
}
}
Good day.
I don't understand exactly what you need.
You want to know when the tornado has applied a $$anonymous$$imum specific force to an object? Or maybe by objects velocity once is inside the tornado?
IF you want to "transferring variables between scripts c#" you should take a look at some tutorial explaining how to do it. Is simple, but you need to learn it by your own, or never will be able to create a game.
Answer by davidcox70 · Apr 10, 2018 at 10:52 AM
Hello, To access variables from another c# script attached to a different object, you first need to get a reference to the gameobject that holds the script, then a reference to the script and then the variable you want to read / write to. So let's say you wanted to access the variable "pullForce" which is a public variable in a script called "tornadoScript", which is attached to a game object to which you can reference with the local gameObject "tornado", then you would do;
float theValue=tornado.GetComponent<tornadoScript>().pullForce;
Only variables marked as "public" can be accessed by other scripts.
void Destroy$$anonymous$$e () { if(float > strength = TornadoVortex.GetComponent ().PullStrength;) { isCloned = true; Destroy (gameObject);
so i got a few errors what am i doing wrong? im trtubg ti sat that if PullStrength is greater than strength it will destroy the gameobject
You have some syntax errors in there I would think;
void Destroy$$anonymous$$e () {
if(strength < TornadoVortex.GetComponent <addYourScriptNameHere>().PullStrength) {
isCloned = true;
Destroy (gameObject);
}
its giving me an error:
void Destroy$$anonymous$$e () { if (strength < TornadoVortex.GetComponent ().PullStrength) { isCloned = true; Destroy (gameObject);
What error is it giving and for which line?
Double check that you have copied it correctly. The code in your last comment isnt the same. The correct syntax is
GetComponent<scriptName>().variableName.
if (strength < TornadoVortex.GetComponent ().PullStrength)
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class destructible : $$anonymous$$onoBehaviour {
public GameObject debrisPrefab;
public float strength;
private bool isCloned;
void On$$anonymous$$ouseDown() {
Destroy$$anonymous$$e ();
}
void OnCollisionEnter(Collision collision ) {
if (collision.relativeVelocity.magnitude > strength) {
Destroy$$anonymous$$e ();
}
}
void Destroy$$anonymous$$e () {
if (strength < destructible.GetComponent <TornadoVortex> ().PullStrength) {
if (debrisPrefab && !isCloned) {
Instantiate (debrisPrefab, transform.position, transform.rotation);
}
isCloned = true;
Destroy (gameObject);
}
}
}
Looks ok. What error is it giving? If it is saying Null Reference then it will be because "destructible" is not connected to your game object. You could test it by adding "Debug.Log(destructible.transform.position)". When run, the console should spit out the position of this game object, if it is connected to it.
ok i fixed the problem for that statement but now its saying the problem is with the 'if' in the statement ( if (debrisPrefab && !isCLoned) { im guessing i put it the statements in the wrong order
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class destructible : $$anonymous$$onoBehaviour {
public GameObject debrisPrefab;
public float strength;
private bool isCloned;
void On$$anonymous$$ouseDown() {
Destroy$$anonymous$$e ();
}
void OnCollisionEnter(Collision collision ) {
if (collision.relativeVelocity.magnitude > strength) {
Destroy$$anonymous$$e ();
}
}
void Destroy$$anonymous$$e () {
if (strength < (destructible.GetComponent <TornadoVortex> ().PullStrength);
if (debrisPrefab && !isCloned) {
Instantiate (debrisPrefab, transform.position, transform.rotation);
}
isCloned = true;
Destroy (gameObject);
}
}
Sometimes the error is caused by the line before not being formed properly with the right amount of brackets, semi colons etc. Look at the if (strength... line. You have an extra "(" before "destructible" so c# is expecting a ")" before the next line. Just loose that extra bracket.
Answer by Jdogmaster · Apr 10, 2018 at 04:23 PM
Alright thanks man I’ll let you know how it goes when I get home from school
ok now its saying this error:
Assets/scripts/destructible.cs(23,32): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.GetComponent()'
You have a class declaration of
public class destructible
... and are later calling
destructible.GetComponent<TornadoVortex>()
GetComponent can't be called on a static class (i.e. destructible), as that's not a GameObject (or similar $$anonymous$$onoBehaviour-supporting class instance).
$$anonymous$$ake certain you're requesting the TornadoVortex component from the correct target object, seeing how destructible also does not contain the PullStrength variable regardless.
ya so i added pullstrength after i posted this so it isnt shown. so in the statement: void Destroy$$anonymous$$e () { if (GetComponent().PullStrength; and i want to find a way to say that if 'PullStrength' from the TornadoVortex script is greater than the strength from this script then something will happen, but i get errors when trying to add the greater than sign
Answer by oroora6_unity · Apr 10, 2018 at 11:23 PM
Ok, let me make an example for you, lets say we have two Classes:
public class FirstClass : MonoBehaviour
{
public int firstClassInt;
}
public class SecondClass : MonoBehaviour
{
}
now lets say we want to access from the "SecondClass" the firstClassInt. First of all we need to make sure that the firstClassInt is a public variable by adding "public" in front of it, then we need to get a reference of the FirstClass inside the SecondClass, since we're in unity a quick way to do this is by passing it through inspector, so we first need a serialized variable that can contain an instance of the first class inside the second class:
public class FirstClass : MonoBehaviour
{
public int firstClassInt;
}
public class SecondClass : MonoBehaviour
{
[SerializeField] FirstClass firstClassInstance;
}
Now all we need to do is go into the inspector and drag and drop the fistClass instance we want to access inside the "First Class Instance" field that just appeard on the SecondClass inspector, after that we can access to the first class variables and methods from the second class by typing:
public class SecondClass : MonoBehaviour
{
[SerializeField] FirstClass firstClassInstance;
private void Start()
{
firstClassInstance.firstClassInt = 1;
}
}
now the firstClassInt value will be 1. Have fun!
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TornadoVortex : $$anonymous$$onoBehaviour {
private GameObject PullOBJ;
public float PullSpeed;
public float objRotationSpeed;
public float rotation;
public float PullStrength;
public void OnTriggerStay (Collider coll)
{
if (coll.gameObject.tag == "Untagged") {
PullOBJ = coll.gameObject;
PullOBJ.transform.position = Vector3.$$anonymous$$oveTowards (PullOBJ.transform.position, this.transform.position, PullSpeed * Time.deltaTime);
PullOBJ.transform.RotateAround (transform.position, Vector3.up, Time.deltaTime * rotation);
PullOBJ.transform.Rotate (Vector3.left, 45 * Time.deltaTime * objRotationSpeed);
}
public class destructible : $$anonymous$$onoBehaviour
{
[SerializeField] destructible destructibleInstance;
}
}
}
im getting errirs with the brackets around SerializeField
Classes are what you call "scripts", what you're doing right now is wrong
wait what you mean? btw this is getting errors right now. im trying to say that if the 'pullstrength' from the TornadoVortex script is greater than the 'strength' from this script then something will happen but i cant figure out how to write this statement if (GetComponent().PullStrength > strength {
Your answer
Follow this Question
Related Questions
Unity createNew Game Object after calling a Destroy() 1 Answer
Script doesn't work anymore! 2 Answers
How do I Destroy a Child after Instantiating it? 1 Answer
Destroy Objects after a set time 2 Answers
Destroying Assests Not Permitted 1 Answer