Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How Can I Reach to a Variable From Another Script

Discussion in 'Scripting' started by SoftwareEngineer, Apr 4, 2014.

  1. SoftwareEngineer

    SoftwareEngineer

    Joined:
    Jan 4, 2014
    Posts:
    52
    Unity give an error like this : An object reference is required to access non-static member.

    I need to change astroid's speed in the GameController script but i have declared this speed in different script moreover i do not know how i can reach to AstroidSpeed scrpit.

    GameController Script

    using UnityEngine;
    using System.Collections;


    public class GameController : MonoBehaviour
    {
    public GameObject hazard;
    public Vector3 spawnValues;
    public int hazardCount;
    public float spawnWait;
    public float startWait;
    public float waveWait;


    void Start()
    {
    StartCoroutine (SpawnWaves ());
    }

    public IEnumerator SpawnWaves()
    {

    yield return new WaitForSeconds(startWait);
    while (true)
    {
    for (int i = 0; i < hazardCount; i++)
    {
    Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    Quaternion spawnRotation = Quaternion.identity;
    Instantiate (hazard, spawnPosition, spawnRotation);
    yield return new WaitForSeconds (spawnWait);
    }
    hazardCount = hazardCount * 2;
    -----> the line which i have problem //AstroidSpeed.astroidSpeed -= 0.05f;
    if(spawnWait > 0.1)
    spawnWait = spawnWait - 0.05f;
    yield return new WaitForSeconds(waveWait);
    }
    }
    }

    AND this is AstroidSpeed script

    using UnityEngine;
    using System.Collections;

    public class AstroidSpeed : MonoBehaviour{

    public float astroidSpeed;
    void start()
    {
    rigidbody.velocity = transform.forward * astroidSpeed;
    }
    }

    Sorry for my english, thanks for answers.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    use
    Code (csharp):
    1.  [ /code] tags please... makes it much easier to read the code you paste in.
    2.  
    3. Have a look at the scripting reference for Instantiate, it has an example of how to get a reference for the object you instantiated and set some of it's variables at the bottom:
    4.  
    5. http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html
     
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Code (csharp):
    1. newObject.gameObject.GetComponent<myScript>().publicVariable;
     
  4. SoftwareEngineer

    SoftwareEngineer

    Joined:
    Jan 4, 2014
    Posts:
    52
    Sorry but i dont understand. Can you explain how i can use it?
     
  5. GilesC9

    GilesC9

    Joined:
    Jun 13, 2013
    Posts:
    7
    Code (csharp):
    1. hazard.gameObject.GetComponent<AstroidSpeed>().asteroidSpeed = 0.05f;
    Hazard is the object for which the asteroid speed script is attached to. That line of code basically tells that specific object to get it's AsteroidSpeed script and then set the asteroidSpeed variable to 0.05f.