'Rotating an Instantiated object
I've hit a bit of a wall with instantiated objects. What I need the engine to do is spawn three prefabs, then at specific times later, flip them over. However, I've been unable to get it working. The spawn code works but when I try adding more to manipulate the prefab, it just gives me loads of errors. The spawn script is inside void Start() while the other script will be in void Update()
Spawn Script:
cardAOne = Instantiate(CardDisplayer.cardList[Shuffle.playerAdeck[0]], new Vector3 (xPosition, yPosition, (zPosition + 3)), Quaternion.Euler(xRotation, yRotation, zRotation)) as GameObject;
cardATwo = Instantiate(CardDisplayer.cardList[Shuffle.playerAdeck[1]], new Vector3 (xPosition, yPosition, (zPosition)), Quaternion.Euler(xRotation, yRotation, zRotation))as GameObject;
cardAThree = Instantiate(CardDisplayer.cardList[Shuffle.playerAdeck[2]], new Vector3 (xPosition, yPosition, (zPosition - 3)), Quaternion.Euler(xRotation, yRotation, zRotation))as GameObject;
cardBOne = Instantiate(CardDisplayer.cardList[Shuffle.playerBdeck[0]], new Vector3 ((0 - xPosition), yPosition, (zPosition + 3)), Quaternion.Euler(xRotation, (0 - yRotation), zRotation))as GameObject;
cardBTwo = Instantiate(CardDisplayer.cardList[Shuffle.playerBdeck[1]], new Vector3 ((0 - xPosition), yPosition, (zPosition)), Quaternion.Euler(xRotation, (0 - yRotation), zRotation))as GameObject;
cardBThree = Instantiate(CardDisplayer.cardList[Shuffle.playerBdeck[2]], new Vector3 ((0 - xPosition), yPosition, (zPosition - 3)), Quaternion.Euler(xRotation, (0 - yRotation), zRotation))as GameObject;
Movement Script:
if (turnA == true) {
cardAOne.transform.localRotation = Quaternion.Euler(0,0,180);
cardATwo.transform.localRotation = Quaternion.Euler(0,0,180);
cardAThree.transform.localRotation = Quaternion.Euler(0,0,180);
}
Solution 1:[1]
The error is most likely here:
if (turnA == true) {
...
I think that you just didn't initialize turnA before accessing it. Maybe you have some branching code where not every branch assigns a value to that variable.
Concerning coding style:
if (turnA) is equivalent to if (turnA == true), but clearer and more conventional.
Solution 2:[2]
A bit of a late answer to this but oh well. No idea what the actual issue was, but updating unity worked. Sometimes it helps to have the most recent software version!
Solution 3:[3]
From reading the comments you just need proper reference to the values. There are a few ways of doing this, statics are probably the quickest and easiest.
Set the cards in the spawn script to public static GameObject cardAOne;,
then call them from movement script with SpawnScript.cardAOne.transform.position.
You could also have a public SpawnScript at the top of your movement file, and you can then drag an drop the instance of it in the inspector from within Unity.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Raimund Krämer |
| Solution 2 | MechaScoots |
| Solution 3 | Patrick |
