Im new in unity and I have a problem regarding arrays.
I want to be able to set the Elements of the arrays via script.
Here straight is a "public gridElementScript[] straight;"
If I'm understanding correctly, you're asking how to access Straight from another instance. If so, you need to cast your instantiated prefab to a GameObject and call GetComponent<MyComponent>() on it. Something like I've written below should work for you.
GameObject object = (GameObject)Object.Instantiate(myPrefab)
GridElementScript script = object.GetComponent<GridElementScript>();
script.Straight[0] = neighbor
Related
So I'm currently spawning a instance of a prefab GameObject, and I want to initialize some of its variables. Usually I would use FindObjectOfType, however the objects I want to attach to it don't have a type, and are pretty much just empty GameObjects with a few children. I feel like the answer should be incredibly obvious, but I can't wrap my head around a solution. Any help would be greatly appreciated.
Edit: I forgot to mention that the GameObjects I want to apply to the instance's variables are inactive at the time I want to do so.
If you are instantiating the object then wanting to set the variables, then just spawn the object and grab the reference.
YourScriptHere tmp = Instantiate(yourObject).GetComponent<YourScriptHere>();
tmp.SetTheFieldHereEqualTo = someValue;
If you do not want to just set references to an object you are spawning, can you clarify your question a bit?
Sorry if my question wasn't super clear, but I managed to find a solution. I basically put the objects I want to set as the variables in parent object which is always active, then through the script I get the parent object, and finally I get its first child, which would be the object I wanted in the first place:
GameObject variable;
GameObject variableParent;
private void Start()
{
variableParent = GameObject.Find("variableParentName");
variable = variableParent.transform.GetChild(0).gameObject;
}
I am trying to instantiate a coin prefab that appears when an enemy dies.
To initially get a reference to the prefab, I retrieved it using its tag:
private GameObject coinSpawn;
Start(){
coinSpawn = GameObject.FindGameObjectWithTag("xxx");
}
Now when the enemy dies, I call Instantiate() as follows:
Instantiate(coinSpawn, transform.position, Quaternion.identity);
However, when I was playing the game and an enemy died, nothing was spawned and I got this error:
ArgumentException: The Object you want to instantiate is null.
I do not understand what I did wrong - I confirmed that I have properly tagged the prefab which I want to instantiate.
One more thing is that I want to do this without making a Resources folder. Is that possible?
Unfortunately, GameObject.FindGameObjectWithTag() only retrieves active GameObjects with the given tag that already exist in the current scene. It sounds like you're trying to use this method to retrieve a reference to a prefab object in your Assets folder, which is something it can't do. As a result, the method is returning null and assigning it to coinSpawn, leading to the error when you try to use coinSpawn later.
You likely need the Resources.Load() method, to which you can supply a path to an object in your Assets folder. Just use that in place of GameObject.FindGameObjectWithTag():
private GameObject coinSpawn;
Start(){
coinSpawn = Resources.Load("path_to_prefab") as GameObject;
}
And yes - you will have to use the name of the prefab instead of its tag to identify it, but I don't think that should pose any problems. (If it does, please update your question accordingly with an explanation.)
Hope this helps! Let me know if you have any questions.
Every time i needed a prefab i would just instantiate it from my prefabs folder and yada yada everything is working perfectly.
But now im trying something different.
I have a class Player() wich has lets say a prop called public int MoveSpeed{get;set;}.
Ok now i want to instantiate a prefab "myPlayer" associated with my Player class. So i could do things like myplayer.Movespeed = x;
How do i use the instantiate(in this scenario)?
How do i use the constructor Player myPlayer = new Player(moveSpeed = x);
I have been searching about this in a lot of places, bue the answers were foggy to me, can anyone help me?
EDIT: I think what im trying to do has no logic. Unity works witha a Component-Model Paradigm, and im trying to go againt that it seems.
The reason why i am asking this is because this time i want to have a solide, smart and flexible code, not a spagheti code mess.
You could instantiate an empty GameObject and build on it, prefabs are just shortcuts so you don't have to write tons of line of code to build up the components on gameobject, here is example:
GameObject someGO = new GameObject();
Player refToClass = someGO.AddComponent<Player>();
refToClass.Movespeed = x;
I've tried multiple ways of initializing the object, and the thing just wont work. I think the problem may lie in one of my other classes, so I've put all the important ones in pastebin.
THE CLASS THROWING THE ERROR: http://pastebin.com/Pj04kUbj
THE TYPE CLASS OF THE OBJECT THROWING THE ERROR:
public class PlayerCharacter : BaseCharacter {
}
THE SUPERCLASS OF THE OBJECT: http://pastebin.com/GAmSMXbh
(damn thing wont let me post more than 2 links)
I doubt this will be solved here, as this is kind of a massive infodump, but if its something really simple and you guys catch it that would be awesome. In the meantime, ill keep looking, but im having little luck
You cannot create a GameObject in that way:
If you use new operator to create PlayerCharacter, you will not have a Unity GameObject at the end. You must Instantiate from a prefab or clone from another GameObject.
On the other hand, you cannot call Awake directly because the Unity GameObject is not built. This function is called from Unity when the GameObject is built for its first time.
See here for an example of creating GameObjects.
EDIT:
you must add the script component to your PlayerCharacter like this:
_toon.AddComponent( PlayerCharacter ) as PlayerCharacter
in that way the script is attached to your GameObject because using new to create it, the script is not attached.
My question is simple, how to access my gameobjects custom C# script through a foreach.
I've tried following:
foreach(TowerController go in GameObject.FindGameObjectsWithTag("Tower")) {
go.upgradeTowerProgress = false;
}
Doesn't seems to work. Dunno why, that's why im here to ask! xD
TowerController is my custom script on the gameObjects with tag Tower.
This should work (untested):
foreach(GameObject go in GameObject.FindGameObjectsWithTag("Tower")) {
go.GetComponent<TowerController>().upgradeTowerProgress = false;
}
What I changed is that you are iterating a GameObject array, so you need to do foreach(GameObject go..., then you use the GetComponent function to get the component that is of the type TowerController and then you can access its updateTowerProgress. THIS page is always a good reference.