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 /
avatar image
0
Question by Jdogmaster · Apr 10, 2018 at 03:33 PM · destroydestroy objectdestroygameobjectpublic variabledestructible

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);
         }
     }
     
     

}

@davidcox70

Comment
Add comment · Show 1
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 tormentoarmagedoom · Apr 10, 2018 at 08:01 AM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 13 · 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 Jdogmaster · Apr 10, 2018 at 01:12 PM 0
Share

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

avatar image davidcox70 Jdogmaster · Apr 10, 2018 at 03:52 PM 0
Share

You have some syntax errors in there I would think;

 void Destroy$$anonymous$$e () {
   if(strength < TornadoVortex.GetComponent <addYourScriptNameHere>().PullStrength) {
   isCloned = true;
   Destroy (gameObject);
 }
avatar image Jdogmaster · Apr 10, 2018 at 10:05 PM 0
Share

its giving me an error:

void Destroy$$anonymous$$e () { if (strength < TornadoVortex.GetComponent ().PullStrength) { isCloned = true; Destroy (gameObject);

avatar image davidcox70 Jdogmaster · Apr 10, 2018 at 10:06 PM 0
Share

What error is it giving and for which line?

avatar image davidcox70 davidcox70 · Apr 10, 2018 at 10:10 PM 0
Share

Double check that you have copied it correctly. The code in your last comment isnt the same. The correct syntax is

 GetComponent<scriptName>().variableName.
avatar image Jdogmaster · Apr 10, 2018 at 10:09 PM 0
Share

if (strength < TornadoVortex.GetComponent ().PullStrength)

avatar image Jdogmaster · Apr 10, 2018 at 10:11 PM 0
Share

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);
     }
 }

}

avatar image davidcox70 Jdogmaster · Apr 10, 2018 at 10:15 PM 0
Share

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.

avatar image Jdogmaster · Apr 10, 2018 at 10:17 PM 0
Share

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);
     }
 }
avatar image davidcox70 Jdogmaster · Apr 10, 2018 at 10:34 PM 0
Share

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.

Show more comments
avatar image
0

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

Comment
Add comment · Show 3 · 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 Jdogmaster · Apr 10, 2018 at 10:46 PM 0
Share

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()'

avatar image Eno-Khaon Jdogmaster · Apr 10, 2018 at 11:09 PM 0
Share

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.

avatar image Jdogmaster Eno-Khaon · Apr 11, 2018 at 12:12 AM 0
Share

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

avatar image
0

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!

Comment
Add comment · Show 4 · 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 Jdogmaster · Apr 10, 2018 at 11:40 PM 0
Share

thanks! ill let you know how it goes

avatar image Jdogmaster · Apr 10, 2018 at 11:49 PM 0
Share

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

avatar image oroora6_unity Jdogmaster · Apr 11, 2018 at 12:01 AM 0
Share

Classes are what you call "scripts", what you're doing right now is wrong

avatar image Jdogmaster · Apr 11, 2018 at 12:04 AM 0
Share

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

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

84 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

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


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