assign class objects instead of the actual script class to unity game objects in unity c#
up vote
1
down vote
favorite
I want to create a game in Unity C# in which the player will add as much as drones he needs as game objects to the game world. Therefore, I made a drone prefab and later in runtime I ask the number of drones the player wants to have (e.x. n numbers) and instantiate the prefab n times.
The script which is attached to the drone prefab is called drone_script.
Therefore, I am having a drone_script class as a general class. This class has an attribute (let's call it subscriber) which should have different unique values for each drone. So, it is created as a null reference in drone_script general class and later in runtime, I will initialize it.
During runtime, I create n (the same numbers as the drone game objects) objects from this class and assign their subscriber attribute different values.
This is how it looks:
some_class
{
for (int i = 0; i < number_of_Drones; ++i)
{
drones_script[i] = new drone_script();
\ here I am creating n objects of my general drone_script class
drones_script[i].drone_subscriber = a unique value;
\ here I am assigning to each of the drone objects' drone_subscriber attribute a different value.
drco = new Vector3((float)0, (float)1, (float)0);
drones[i] = Instantiate(Resources.Load("drone"), drco,
Quaternion.Euler(-90, 0, 90)) as GameObject;
\I instatiate the drone prefab in unity game world here.
}
}
drone_script
{
public ROSBridgeSubscriber drone_subscriber;
void Start() { \some code }
void FixedUpdate () { \some code }
}
My problem is when the drone game objects are created in unity, the general class drone_script is attached to them and because of this their subscriber attribute is null. However, I need the drone_script[i] object to be assigned to them as their script, not the general drone_script itself. Because only then each drone game object has its corresponding subscriber attribute value from before even the start function is being called.
Am I thinking correctly?
If yes, how can I do it?
c# unity3d
add a comment |
up vote
1
down vote
favorite
I want to create a game in Unity C# in which the player will add as much as drones he needs as game objects to the game world. Therefore, I made a drone prefab and later in runtime I ask the number of drones the player wants to have (e.x. n numbers) and instantiate the prefab n times.
The script which is attached to the drone prefab is called drone_script.
Therefore, I am having a drone_script class as a general class. This class has an attribute (let's call it subscriber) which should have different unique values for each drone. So, it is created as a null reference in drone_script general class and later in runtime, I will initialize it.
During runtime, I create n (the same numbers as the drone game objects) objects from this class and assign their subscriber attribute different values.
This is how it looks:
some_class
{
for (int i = 0; i < number_of_Drones; ++i)
{
drones_script[i] = new drone_script();
\ here I am creating n objects of my general drone_script class
drones_script[i].drone_subscriber = a unique value;
\ here I am assigning to each of the drone objects' drone_subscriber attribute a different value.
drco = new Vector3((float)0, (float)1, (float)0);
drones[i] = Instantiate(Resources.Load("drone"), drco,
Quaternion.Euler(-90, 0, 90)) as GameObject;
\I instatiate the drone prefab in unity game world here.
}
}
drone_script
{
public ROSBridgeSubscriber drone_subscriber;
void Start() { \some code }
void FixedUpdate () { \some code }
}
My problem is when the drone game objects are created in unity, the general class drone_script is attached to them and because of this their subscriber attribute is null. However, I need the drone_script[i] object to be assigned to them as their script, not the general drone_script itself. Because only then each drone game object has its corresponding subscriber attribute value from before even the start function is being called.
Am I thinking correctly?
If yes, how can I do it?
c# unity3d
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I want to create a game in Unity C# in which the player will add as much as drones he needs as game objects to the game world. Therefore, I made a drone prefab and later in runtime I ask the number of drones the player wants to have (e.x. n numbers) and instantiate the prefab n times.
The script which is attached to the drone prefab is called drone_script.
Therefore, I am having a drone_script class as a general class. This class has an attribute (let's call it subscriber) which should have different unique values for each drone. So, it is created as a null reference in drone_script general class and later in runtime, I will initialize it.
During runtime, I create n (the same numbers as the drone game objects) objects from this class and assign their subscriber attribute different values.
This is how it looks:
some_class
{
for (int i = 0; i < number_of_Drones; ++i)
{
drones_script[i] = new drone_script();
\ here I am creating n objects of my general drone_script class
drones_script[i].drone_subscriber = a unique value;
\ here I am assigning to each of the drone objects' drone_subscriber attribute a different value.
drco = new Vector3((float)0, (float)1, (float)0);
drones[i] = Instantiate(Resources.Load("drone"), drco,
Quaternion.Euler(-90, 0, 90)) as GameObject;
\I instatiate the drone prefab in unity game world here.
}
}
drone_script
{
public ROSBridgeSubscriber drone_subscriber;
void Start() { \some code }
void FixedUpdate () { \some code }
}
My problem is when the drone game objects are created in unity, the general class drone_script is attached to them and because of this their subscriber attribute is null. However, I need the drone_script[i] object to be assigned to them as their script, not the general drone_script itself. Because only then each drone game object has its corresponding subscriber attribute value from before even the start function is being called.
Am I thinking correctly?
If yes, how can I do it?
c# unity3d
I want to create a game in Unity C# in which the player will add as much as drones he needs as game objects to the game world. Therefore, I made a drone prefab and later in runtime I ask the number of drones the player wants to have (e.x. n numbers) and instantiate the prefab n times.
The script which is attached to the drone prefab is called drone_script.
Therefore, I am having a drone_script class as a general class. This class has an attribute (let's call it subscriber) which should have different unique values for each drone. So, it is created as a null reference in drone_script general class and later in runtime, I will initialize it.
During runtime, I create n (the same numbers as the drone game objects) objects from this class and assign their subscriber attribute different values.
This is how it looks:
some_class
{
for (int i = 0; i < number_of_Drones; ++i)
{
drones_script[i] = new drone_script();
\ here I am creating n objects of my general drone_script class
drones_script[i].drone_subscriber = a unique value;
\ here I am assigning to each of the drone objects' drone_subscriber attribute a different value.
drco = new Vector3((float)0, (float)1, (float)0);
drones[i] = Instantiate(Resources.Load("drone"), drco,
Quaternion.Euler(-90, 0, 90)) as GameObject;
\I instatiate the drone prefab in unity game world here.
}
}
drone_script
{
public ROSBridgeSubscriber drone_subscriber;
void Start() { \some code }
void FixedUpdate () { \some code }
}
My problem is when the drone game objects are created in unity, the general class drone_script is attached to them and because of this their subscriber attribute is null. However, I need the drone_script[i] object to be assigned to them as their script, not the general drone_script itself. Because only then each drone game object has its corresponding subscriber attribute value from before even the start function is being called.
Am I thinking correctly?
If yes, how can I do it?
c# unity3d
c# unity3d
edited Nov 11 at 21:59
Draco18s
10.1k31940
10.1k31940
asked Nov 10 at 19:18
zahra
174
174
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
First of all, I suggest you to assign dronePrefab using editor or in Start method of "some_class" because Resources.Load("drone") might be resource consuming, since you load a drone resource "n" times.
Now your specific problem, you don't need to create new instances of "drone_script" because as you mentioned yourself, each drone that you create has his own "drone_script". So basically what we need to do is:
- Instantiate drone and save his reference (as GameObject)
- Get that drone's "drone_script" (using GetComponent<>(); )
- Save that script reference in our array so we can use it later
- Adjust some variables of that script now or later in the game
Something like that should work. In code everything should be something like this (didn't test, might be some miss types)
some_class
{
public GameObject dronePrefab;
for (int i = 0; i < number_of_Drones; ++i)
{
// Step 1.
drco = new Vector3((float)0, (float)1, (float)0);
drones[i] = Instantiate(dronePrefab, drco,
Quaternion.Euler(-90, 0, 90)) as GameObject;
// Steps 2 & 3
drones_script[i] = drones[i].GetComponent<drone_script>();
// Step 4
drones_script[i].drone_subscriber = a unique value;
}
}
EDIT:
Just to point it out: drone_script has to be attached to drone prefab
Correction:drones[i].GetComponent<drone_script>();
, the T parameter needs to be the class, not a string of the class's name.
– Draco18s
Nov 10 at 20:20
Thank you so much for your answer. Do I need to attach drone_script to drone prefab too?
– zahra
Nov 10 at 20:34
The solution is not working. I tried it.
– zahra
Nov 10 at 21:40
I've edited as @Draco18s pointed out. Yes, drone_script must be a part of the drone prefab. What is the problem that it is not working ? Debug and write us what is the problem, but I believe it should be ok if everything is done correctly
– rCgLT
Nov 11 at 21:09
You can change the type of dronePrefab to the drone_script if it is a MonoBehaviour and save yourself from having to do GetComponent.
– CaTs
Nov 14 at 1:24
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
First of all, I suggest you to assign dronePrefab using editor or in Start method of "some_class" because Resources.Load("drone") might be resource consuming, since you load a drone resource "n" times.
Now your specific problem, you don't need to create new instances of "drone_script" because as you mentioned yourself, each drone that you create has his own "drone_script". So basically what we need to do is:
- Instantiate drone and save his reference (as GameObject)
- Get that drone's "drone_script" (using GetComponent<>(); )
- Save that script reference in our array so we can use it later
- Adjust some variables of that script now or later in the game
Something like that should work. In code everything should be something like this (didn't test, might be some miss types)
some_class
{
public GameObject dronePrefab;
for (int i = 0; i < number_of_Drones; ++i)
{
// Step 1.
drco = new Vector3((float)0, (float)1, (float)0);
drones[i] = Instantiate(dronePrefab, drco,
Quaternion.Euler(-90, 0, 90)) as GameObject;
// Steps 2 & 3
drones_script[i] = drones[i].GetComponent<drone_script>();
// Step 4
drones_script[i].drone_subscriber = a unique value;
}
}
EDIT:
Just to point it out: drone_script has to be attached to drone prefab
Correction:drones[i].GetComponent<drone_script>();
, the T parameter needs to be the class, not a string of the class's name.
– Draco18s
Nov 10 at 20:20
Thank you so much for your answer. Do I need to attach drone_script to drone prefab too?
– zahra
Nov 10 at 20:34
The solution is not working. I tried it.
– zahra
Nov 10 at 21:40
I've edited as @Draco18s pointed out. Yes, drone_script must be a part of the drone prefab. What is the problem that it is not working ? Debug and write us what is the problem, but I believe it should be ok if everything is done correctly
– rCgLT
Nov 11 at 21:09
You can change the type of dronePrefab to the drone_script if it is a MonoBehaviour and save yourself from having to do GetComponent.
– CaTs
Nov 14 at 1:24
add a comment |
up vote
1
down vote
accepted
First of all, I suggest you to assign dronePrefab using editor or in Start method of "some_class" because Resources.Load("drone") might be resource consuming, since you load a drone resource "n" times.
Now your specific problem, you don't need to create new instances of "drone_script" because as you mentioned yourself, each drone that you create has his own "drone_script". So basically what we need to do is:
- Instantiate drone and save his reference (as GameObject)
- Get that drone's "drone_script" (using GetComponent<>(); )
- Save that script reference in our array so we can use it later
- Adjust some variables of that script now or later in the game
Something like that should work. In code everything should be something like this (didn't test, might be some miss types)
some_class
{
public GameObject dronePrefab;
for (int i = 0; i < number_of_Drones; ++i)
{
// Step 1.
drco = new Vector3((float)0, (float)1, (float)0);
drones[i] = Instantiate(dronePrefab, drco,
Quaternion.Euler(-90, 0, 90)) as GameObject;
// Steps 2 & 3
drones_script[i] = drones[i].GetComponent<drone_script>();
// Step 4
drones_script[i].drone_subscriber = a unique value;
}
}
EDIT:
Just to point it out: drone_script has to be attached to drone prefab
Correction:drones[i].GetComponent<drone_script>();
, the T parameter needs to be the class, not a string of the class's name.
– Draco18s
Nov 10 at 20:20
Thank you so much for your answer. Do I need to attach drone_script to drone prefab too?
– zahra
Nov 10 at 20:34
The solution is not working. I tried it.
– zahra
Nov 10 at 21:40
I've edited as @Draco18s pointed out. Yes, drone_script must be a part of the drone prefab. What is the problem that it is not working ? Debug and write us what is the problem, but I believe it should be ok if everything is done correctly
– rCgLT
Nov 11 at 21:09
You can change the type of dronePrefab to the drone_script if it is a MonoBehaviour and save yourself from having to do GetComponent.
– CaTs
Nov 14 at 1:24
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
First of all, I suggest you to assign dronePrefab using editor or in Start method of "some_class" because Resources.Load("drone") might be resource consuming, since you load a drone resource "n" times.
Now your specific problem, you don't need to create new instances of "drone_script" because as you mentioned yourself, each drone that you create has his own "drone_script". So basically what we need to do is:
- Instantiate drone and save his reference (as GameObject)
- Get that drone's "drone_script" (using GetComponent<>(); )
- Save that script reference in our array so we can use it later
- Adjust some variables of that script now or later in the game
Something like that should work. In code everything should be something like this (didn't test, might be some miss types)
some_class
{
public GameObject dronePrefab;
for (int i = 0; i < number_of_Drones; ++i)
{
// Step 1.
drco = new Vector3((float)0, (float)1, (float)0);
drones[i] = Instantiate(dronePrefab, drco,
Quaternion.Euler(-90, 0, 90)) as GameObject;
// Steps 2 & 3
drones_script[i] = drones[i].GetComponent<drone_script>();
// Step 4
drones_script[i].drone_subscriber = a unique value;
}
}
EDIT:
Just to point it out: drone_script has to be attached to drone prefab
First of all, I suggest you to assign dronePrefab using editor or in Start method of "some_class" because Resources.Load("drone") might be resource consuming, since you load a drone resource "n" times.
Now your specific problem, you don't need to create new instances of "drone_script" because as you mentioned yourself, each drone that you create has his own "drone_script". So basically what we need to do is:
- Instantiate drone and save his reference (as GameObject)
- Get that drone's "drone_script" (using GetComponent<>(); )
- Save that script reference in our array so we can use it later
- Adjust some variables of that script now or later in the game
Something like that should work. In code everything should be something like this (didn't test, might be some miss types)
some_class
{
public GameObject dronePrefab;
for (int i = 0; i < number_of_Drones; ++i)
{
// Step 1.
drco = new Vector3((float)0, (float)1, (float)0);
drones[i] = Instantiate(dronePrefab, drco,
Quaternion.Euler(-90, 0, 90)) as GameObject;
// Steps 2 & 3
drones_script[i] = drones[i].GetComponent<drone_script>();
// Step 4
drones_script[i].drone_subscriber = a unique value;
}
}
EDIT:
Just to point it out: drone_script has to be attached to drone prefab
edited Nov 14 at 0:44
answered Nov 10 at 20:10
rCgLT
562
562
Correction:drones[i].GetComponent<drone_script>();
, the T parameter needs to be the class, not a string of the class's name.
– Draco18s
Nov 10 at 20:20
Thank you so much for your answer. Do I need to attach drone_script to drone prefab too?
– zahra
Nov 10 at 20:34
The solution is not working. I tried it.
– zahra
Nov 10 at 21:40
I've edited as @Draco18s pointed out. Yes, drone_script must be a part of the drone prefab. What is the problem that it is not working ? Debug and write us what is the problem, but I believe it should be ok if everything is done correctly
– rCgLT
Nov 11 at 21:09
You can change the type of dronePrefab to the drone_script if it is a MonoBehaviour and save yourself from having to do GetComponent.
– CaTs
Nov 14 at 1:24
add a comment |
Correction:drones[i].GetComponent<drone_script>();
, the T parameter needs to be the class, not a string of the class's name.
– Draco18s
Nov 10 at 20:20
Thank you so much for your answer. Do I need to attach drone_script to drone prefab too?
– zahra
Nov 10 at 20:34
The solution is not working. I tried it.
– zahra
Nov 10 at 21:40
I've edited as @Draco18s pointed out. Yes, drone_script must be a part of the drone prefab. What is the problem that it is not working ? Debug and write us what is the problem, but I believe it should be ok if everything is done correctly
– rCgLT
Nov 11 at 21:09
You can change the type of dronePrefab to the drone_script if it is a MonoBehaviour and save yourself from having to do GetComponent.
– CaTs
Nov 14 at 1:24
Correction:
drones[i].GetComponent<drone_script>();
, the T parameter needs to be the class, not a string of the class's name.– Draco18s
Nov 10 at 20:20
Correction:
drones[i].GetComponent<drone_script>();
, the T parameter needs to be the class, not a string of the class's name.– Draco18s
Nov 10 at 20:20
Thank you so much for your answer. Do I need to attach drone_script to drone prefab too?
– zahra
Nov 10 at 20:34
Thank you so much for your answer. Do I need to attach drone_script to drone prefab too?
– zahra
Nov 10 at 20:34
The solution is not working. I tried it.
– zahra
Nov 10 at 21:40
The solution is not working. I tried it.
– zahra
Nov 10 at 21:40
I've edited as @Draco18s pointed out. Yes, drone_script must be a part of the drone prefab. What is the problem that it is not working ? Debug and write us what is the problem, but I believe it should be ok if everything is done correctly
– rCgLT
Nov 11 at 21:09
I've edited as @Draco18s pointed out. Yes, drone_script must be a part of the drone prefab. What is the problem that it is not working ? Debug and write us what is the problem, but I believe it should be ok if everything is done correctly
– rCgLT
Nov 11 at 21:09
You can change the type of dronePrefab to the drone_script if it is a MonoBehaviour and save yourself from having to do GetComponent.
– CaTs
Nov 14 at 1:24
You can change the type of dronePrefab to the drone_script if it is a MonoBehaviour and save yourself from having to do GetComponent.
– CaTs
Nov 14 at 1:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242558%2fassign-class-objects-instead-of-the-actual-script-class-to-unity-game-objects-in%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown