Search Unity

Acces public float other script C#

Discussion in 'Scripting' started by TehChriis, Jul 25, 2014.

  1. TehChriis

    TehChriis

    Joined:
    Feb 26, 2014
    Posts:
    16
    Hello!
    I have been searching all over the internet without finding an answer. I tried many techniques but none seem to be working.
    I have a wallspawner, where I want to decide what the speed is of the walls that are falling down.
    Then there is the wall, which obviously contains the speed.

    I need to acces from within the wall prefab, the wallspawn script.. I am seeing alot of 'getCompenent' and stuff but none seem to work for me, or are too 'large' , all I want is acces a public float (why bother making it public if it's such a HUGE trouble accessing public variables)
    But anyways, I can understand code when I see it, but I can't really come up with the solution right now because I am really new to C#. I would love to read a answer about what I have to do, not directions what to read up on because those haven't helped me at all.

    The error I am getting here is a 'NullreferenceException: Object reference not set to an instance of an object"
    which is the code in the start(){ ... } of the wallscript

    Thank you in advance! I will post part of the scripts I have here:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class wallSpawn : MonoBehaviour
    5. {
    6.         public GameObject Wall;
    7.         public float spawnTimeLimit = 1f; // 10 seconds.
    8.         public float timePassed = 0f;
    9.         public float mainSpeed = 5f;
    10.  
    11.         // Use this for initialization
    12.         void Start ()
    13.         {
    14.  
    15.         }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class wallScript : MonoBehaviour
    5. {
    6.  
    7.         private int spawnPos;
    8.         public float wallSpeed;
    9.  
    10.     public wallSpawn wallspawn;
    11.         void Start ()
    12.         {
    13.         wallSpeed = wallspawn.mainSpeed;
    14.         }
    15.  
    16.         void Awake ()
    17.         {
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Check out my video on Getcomponent. you can find it in my signature. That will help you out.
     
    Onolax likes this.
  3. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    You need to use getComponent, attach wallSpawn script to a gameObject (it can be the same gameObject as wallscript) then use getComponent to access the float variable.

    if the wallSpawn script is in the same gameObject it should be something like this:
    Code (CSharp):
    1.  
    2. private wallSpawn wallSpawnScript;
    3.  
    4. void Start(){
    5. wallSpawnScript = gameObject.getComponent<wallSpawn>();
    6. wallSpeed = wallSpawnScript.mainSpeed;
    7. }

    if you attach wallSpawn to a different object:
    Code (CSharp):
    1.  
    2. private wallSpawn wallSpawnScript;
    3. public GameObject wallSpawnObject;
    4.  
    5. void Start(){
    6. wallSpawnScript = wallSpawnObject.getComponent<wallSpawn>();
    7. wallSpeed = wallSpawnScript.mainSpeed;
    8. }
    The code is untested so I may have made typos while writing it but it should be correct.
     
  4. TehChriis

    TehChriis

    Joined:
    Feb 26, 2014
    Posts:
    16
    Umh, So I've been trying to solve this really, but I just can't get it to work rayconsantana.. Your code look perfectly fine but everytime I try to run it, it says 'the variable wallSpawnObject of 'wallScript' has not been assigned.

    Let just add one big image with all the information you need, I can't drag the spawnmanager (the game object the contains the wallSpawn script) to the Wall Spawn Object in the 'wall prefab'.

    this link contains the image :)
    https://www.dropbox.com/s/hkzwl2od51q77v2/unityhelp.png

    please help me solve this once and for all so I understand what is going on :D
     
  5. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Well you forgot to add the spawnmanager in the inspector, click here

    Obviously you cant drag the spawnmanager object in the Wall prefab you can however drag the spawnmanager into an instance of the Wall prefab but if what you want is to assign the spawnmanager object at run time then you need to find the object first, for example using tags.


    add this line at the beginning of your Start() func.
    Code (CSharp):
    1.  
    2. wallSpawnObject = GameObject.FindGameObjectWithTag ("Spawn");
    3.  
    4.  
    Obviously you need to create a new tag called "Spawn" and tag your spawnmanager object with it.
     
    Last edited: Jul 27, 2014
  6. TehChriis

    TehChriis

    Joined:
    Feb 26, 2014
    Posts:
    16
    Aaaaand it's solved! Thank you Raycosantana :D!!

    I'm sure I will see you again soon enough on another question with will surely enough rise. but for now! thanks !
     
    raycosantana likes this.