3

I can access the object from the script however I want to directly change values of the object in the script and this is not allowing me to. Here is my scrip for the main script:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using RTS;

public class QuestionMenu : MonoBehaviour {
public struct Question{
    public string questionText;
    public string answer;
    public string solution1;
    public string solution2;
    public string solution3;
}
//public void QuestionObject(string QuestionText, string answer, string solution1, string solution2, string solution3){
    //Debug.Log ("lol");
//}
public Question QuestionObject = new Question();
Canvas canvas;
public UserInput Player;
public Text QuestionText;
public Text Button1;
// Use this for initialization
void Start () {
    canvas = GetComponent<Canvas>();
    canvas.enabled = false;
    ResourceManager.QuestionMenuOpen = false;
    //instruction = GetComponent<Text>();
    //Debug.Log (QuestionText);
    Button1.text = "Crazy";
    QuestionText.text = "Yo Bro";
}

// Update is called once per frame
void Update () {
    ////Debug.Log (QuestionObject.questionText);
    if(ResourceManager.QuestionMenuOpen == true){
        //QuestionObject.questionText = "bloo";
        Pause ();
        //QuestionText.text = "Question 1) Differentiate 3x\u2074 + 2x.";
        //Button1.text = "12x\u00B3 + 2";
    }
}

public void Pause (){
    canvas.enabled = true;
    Player.enabled = false;
    Time.timeScale = 0.0f;
    Cursor.visible = true;
    //QuestionObject = Questions.QuestionObject;
    Debug.Log (QuestionObject.questionText);
    QuestionText.text = QuestionObject.questionText;
    //ResourceManager.QuestionMenuOpen = true;
    }

void Resume (){
    Cursor.visible = false;
    canvas.enabled = false;
    Player.enabled = true;
    Time.timeScale = 1.0f;
    ResourceManager.QuestionMenuOpen = false;
    }


}

And here is the script for my script:

using UnityEngine;
using System.Collections;

public class QuestionScript : MonoBehaviour {
// Use this for initialization
void Start () {
    QuestionMenu.QuestionObject.answer = "asdf";
}

// Update is called once per frame
void Update () {

}
}

Edit: I wish to access the Object QuestionObject from the scrip shown above here is a screen shot of the gameObject which has both scripts attached to it

enter image description here

2
  • you want to access the Question struct defined in parent class in child class? Commented Nov 18, 2015 at 9:56
  • Yep - I want to change the values within the gameObject called QuestionObject (defined immidiately after the struct) Commented Nov 18, 2015 at 9:58

2 Answers 2

2

I think you misunderstood the Inheritance concept. When you declare a class like you do here :

public class Questions : QuestionMenu {

it means Question class has the same functions and variables with QuestionMenu class (I assume they are all public). But it doesnt mean you can connect 2 GameObjects to each other like that. When you set Question script on Parent that is another object (I mean Question object). And when you set QuestionMenu script on the Child it is totaly different and new object. Also they have no connection with eachother. What you should do is :

using UnityEngine;
using System.Collections;

public class Questions {
public Questions dpoint; // u can set this on unity editor by just draging and dropping the parent object here.

void Start () {
    //or you can get it from code I belive
    dpoint = transform.parent.GetComponent<Question>();
    QuestionObject.questionText = "asd";

}

// Update is called once per frame
void Update () {    
    base.QuestionObject.questionText = "asd2";
}

}

I hope that I didnt misunderstand your question..

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks I understand it better now but how can I access the GameObject QuestionObject as it is not in the scene - is there a way of accessing it like this - as I used your code and it could not find Question
I think even im confused now as to what the question was in the first place.
So you mean the parent object doesnt have Question script ?
Probably we are missing what he exactly ask
Edited it at the bottom to make it more clear and updated the 2nd script code to show my current attempts (note the scripts in the gameObject are located at the bottom right of the image) - also the QuestionObject is now declared as static as LumbusterTick suggestion
|
2

USE public static Question QuestionObject = new Question();

then you can access and change its values in child class like this

Parentclass.QuestionObject.value

3 Comments

Ok - ive done that but now I get an error unless i take out the line base.QuestionObject.questiontext, also using just QuestionObject.questionText still doesn't seem to be making a difference - perhaps the child script is never called - and if so how could i call it from the parent script
you call other scripts like how @Sir.Walek answered.
what do you mean perhaps the child script is never called? If its attached to a gameobject and active in the scene its being run and will change the value .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.